GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing ss64.com — How-To: Run a bash shell script - Linux How-to: Run a bash shell scriptA shell script is a text file containing one or more commands. #!/bin/bash The first line contains a shebang #! followed by the path to the shell, in this case bash - this acts as an interpreter directive and ensures that the script is executed under the correct shell. The "#" character indicates a comment, so the shebang line is ignored by bash when running the script. Next you need to make the script executable with chmod $ chmod u+x my_script1.sh You can now run the script by prefixing it with ./ $ ./my_script1.sh Naming the file with an .sh extension is not required, but you may find it a helpful reminder that the file is a shell script. If you will be writing a few shell scripts then it’s worth creating a folder, perhaps called "scripts" and adding that to the system path: $ mkdir -p $HOME/scripts Even better is to edit your .bash_profile file to include export PATH="$PATH:~/scripts" that will keep the "scripts" folder in your path every time you log in. With the script saved in the folder, you can now run it with just: $ my_script1.sh If this returns an error like #: bad interpreter: No such file or directory that is typically an indication that the File encoding or line endings are wrong, use an editor like VI or just make sure the text is plain ASCII/Unicode and the line endings are classic UNIX LF (not Windows CR/LF or Mac CR) Passing parameters to a shell script: $ cat myscript2.sh Running scripts from the system path
Most versions of Unix make this a part of the Operating System’s command execution mechanism. If the first line of a script begins with the two characters '#!', the remainder of the line specifies an interpreter for the program. Thus, you can specify Bash, The arguments to the interpreter consist of a single optional argument following the interpreter name on the first line of the script file, followed by the name of the script file, followed by the rest of the arguments. Bash will perform this action on Operating Systems that do not handle it themselves. Note that some older versions of Unix limit the interpreter name and argument to a maximum of 32 characters. Bash scripts often begin with the shebang Adding the following two set options to your scripts can be useful to catch undefined variables ("") and to exit if a command fails: #!/bin/bash Errors: /bin/bash^M: bad interpreter: no such file or directory Related Linux commandsBASH Syntax Copyright © 1999-2026 SS64.com
Some rights reserved |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |