A script is a program written in an interpreted language, typically associated with a shell or other program whose primary purpose is something other than as an interpreted language.
A shell script is a script associated with a shell used for automating shell commands.
is a plain text file
Creating a Shell Script
Begins with a shebang, which is a line that identifies the shell used to run the script.
aka hashbang, hashpling, or pound bang
e.g., #!/bin/bash
first two characters tell the Linux kernel that this is a script
pathname tells which program to run it
# is a comment
/bin/sh is typically a symbolic link that points to some other shell
usually /bin/bash
must make file executable when done
chmod +x scriptname
to run a script in current directory:
./scriptname
otherwise will search the PATH
can move the script to directory in the PATH to run script from anywhere
Arguments
A variable is a placeholder in a script for a value that will be determined when the script runs.
can be
passed as parameters to a script
called parameters or arguments
represented by a $ followed by a number starting at 0
$0 is the command name itself, $1 is the first argument, and so on
e.g., for script myuser: useradd -m $1
mkuser username
generated internally
or extracted from a script’s environment
can assign the output of a command to a variable by setting it in backticks