To Bash or not to Bash
Writing shell scripts can automate command execution and help us become more productive. Bash is the default shell for most linux distributions.
Which Bash
Check that your have Bash installed.
$ which bash
/usr/bin/bash
Bash process
To check which shell that you are currently using, we can use ps to report the current shell process.
$ ps -p $$
PID PPID PGID WINPID TTY UID STIME COMMAND
143 88 143 12484 pty0 197609 21:39:51 /usr/bin/bash
$SHELL variable
Alternatively, you can echo the $SHELL variable which specifies your user default shell. It is only true if you have not switched your shell since login.
$ echo "$SHELL"
/usr/bin/bash
Security
Do not run any scripts from untrusted sources! Always avoid running scripts with root or sudo unless you know what it does.
Basic bash script structure
Usually a shell script has the file extension .sh
, but it can be anything. It is just a text file that is up to the shell to interpret the contents.
#!/bin/bash
echo "Hello World"
echo 'I am also a string'
A bash script should always start with #!/bin/bash
. Although the script will run just fine without it, it is a convention that indentifies itself as a bash script.
Assuming the shell script above is store in a file named hello.sh
, to execute the bash script:
$ ./hello.sh
Hello World
I am also a string
Remember to add execution permission to the script.
For example:chmod +x hello.sh