Difference between commands in bash script and commands in terminal
Solution 1
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.
Solution 2
In general, the answer would be "no", commands in shell are the same in scripts, in syntax and semantics.
But there is a bunch of small nuances related to configuration of environment (what variables are used and to what they are set).
the interactive shell of choice for Linux is bash, but scripting often uses other interpreters (
sh
, which is a predecessor ofbash
,ksh
, which is on par with bash), so you have to take into account what shell is used (the current shell s name is traditionally held in variableSHELL
, try typingecho $SHELL
).there may be differences in configuration of the same interpreter for interactive session and for script execution.
Related videos on Youtube
jth41
Updated on September 18, 2022Comments
-
jth41 almost 2 years
Are there any differences between commands that you type into the terminal and commands you include in a script?
-
michas over 11 yearsNo. Pasting the content is equivalent to sourcing the script. This will change all kind of options in the current shell, which would not be changed by simply running the script.
-
Mathieu J. over 11 yearsyou are correct. but as far as his questions goes. I think we can say it is quite similar. but yes, to translate what michas said in noob'er terms. pasting the content of the script is equivalent to do
source script_file
. which will edit ENV vars in the current context. while running the script as./script_file
will not modify the ENV, unlessexport
is used explicitly. -
michas over 11 yearsNo. Even with
export
it is not possible for a script to modify any variables of the calling shell. The same goes for things like PWD, defined aliases, functions, and things like that. They can be changed only interactively or by sourcing a file. But yes, that is probably nothing someone will stumble upon the first day. :)