13

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

1

3 Answers 3

8

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.

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 of bash, 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 variable SHELL, try typing echo $SHELL).

  • there may be differences in configuration of the same interpreter for interactive session and for script execution.

0

No. A script is a list of commands you can type in the terminal.

You can paste the totality of a script in the terminal, and the result will be the same as running it.

Inversely, you can "save" your terminal commands inside of a file and turn it into a reusable script and share it with your family and friends.

4
  • 4
    No. 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.
    – michas
    Dec 26, 2012 at 23:44
  • you 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, unless export is used explicitly.
    – Mathieu J.
    Dec 27, 2012 at 2:34
  • 1
    No. 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. :)
    – michas
    Dec 27, 2012 at 8:29
  • 1
    @michas: Well, it is still better to give the correct description. If the beginner is ambitious, and has work habits, he or she will pick up quickly. If we try to tell the child's tale, we'll just stumble upon our feet and make an incomprehensible impression -- besides, the beginner may be back, later. While I don't consider myself a beginner, there are a lot of things I don't know, and I would be insulted if anyone withheld anything answering my questions because "you're not there yet, junior". Dec 28, 2012 at 2:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .