Linux – Difference between commands in bash script and commands in terminal

commandlinuxscriptingterminal

Are there any differences between commands that you type into the terminal and commands you include in a script?

Best Answer

Your terminal runs a shell (most probably bash), which is the one asking for your commands and runs them.

Besides of this interactive mode you can also use your shell to run commands from a file. To execute the commands in your file you can either call the shell directly like bash script or you can start your file with a "shebang" #!/bin/bash and make it executable (chmod +x script). Then you can treat the script like a binary and execute it for example as ./script or put it at a place where you shell is looking for it. (echo $PATH)

Most probably both your interactive shell and the shell used to run is bash. From the perspective of a "first day learning linux" bash works exactly the same in both modes. - Much later you might stumble about slight differences. If you really want to know about in detail I would suggest reading man bash and search for places mentioning "interactive". (You can search a man page, by pressing /.)

One important thing to note is that the script is run as a new process. This especially means that variables set in the script are not visible in the calling shell.

$ export a=1
$ echo $a
1
$ bash -c 'echo $a;a=2;echo $a' # change the value in a script
1
2
$ echo $a # value not changed here
1
$ 

Without the export a is not even visible to the inner script.

Related Question