Shell Scripting

To make Command-line entries will be preceded by the Dollar sign ($).

PS1="$ " ; export PS1

For more info about PS1, PS2, PS3, PS4 and prompt_command examples, see here

Do you know when you write

$ echo hello world

echo consider this as two parameters, or arguments - first is "Hello", and second is "World".

so even you write

$ echo hello           world

it will still print "hello world"

$ ls
$ mkdir Dir_{0,1,2,3,4,5}
$ ls
Dir_0    Dir_1    Dir_2    Dir_3    Dir_4    Dir_5
$

Sample shell script,

#!/bin/sh             ----# First Line, A special directive (start with #!), standard location of shell
# This is a comment!  ----# comment start with #
VAR1="Hello World"    ----# shouldn't be any space between variable name and value
echo $VAR1

Commands

for -> loops iterate through a set of values until the list is exhausted:

file1.sh

Output as below:

While

file2.sh

file3.sh

Test => [ ]

In bash you can do help test to see test options.

Need to put spaces around all your operators.

$0 .. $9 , $@ , $*, $# , $? , $$ and $!

The variable $0 is the basename of the program as it was called.

$1 .. $9 are the first 9 additional parameters the script was called with.

The variable $@ is all parameters $1 .. whatever.

The variable $*, is similar, but does not preserve any whitespace, and quoting, so "File with spaces" becomes "File" "with" "spaces".

As a general rule, use $@ and avoid $*.

$# is the number of parameters the script was called with.

$? contains the exit value of the last run command.

$$ variable is the PID (Process IDentifier) of the currently running shell.

$! variable is the PID of the last run background process.

$IFS This is the Internal Field Separator. The default value is SPACE TAB NEWLINE

For more Info : http://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html#Special-Parameters

Always use with double quotes, like old_IFS="$IFS" instead of old_IFS=$IFS.

By using curly braces and the special ":-" usage, you can specify a default value to use if the variable is unset.

":=" which sets the variable to the default if it is undefined.

backtick (`)is often associated with external commands. The backtick is used to indicate that the enclosed text is to be executed as a command.

` (backtick) runs in a subshell

shell function cannot change its parameters, though it can change global parameters.

tr translated the spaces into octal character 012 (NEWLINE).

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Last updated

Was this helpful?