Bash Vs KSH

Bash Vs KSH

Linux and Unix have various shells. Two kinds of these numerous shells are KSH and BASH.

KSH (The Korn Shell) was developed many years before the BASH. Ksh has associative arrays and handles loop syntax better than bash. Also ksh’s command print is better than bash’s echo command. In other way, ksh does not support history completion, process substitution and rebindable command-line editing.

Bash has more added extension than ksh. Bash has tab completion and easier method to set a prompt in order to display current directory.

Compared to ksh, bash is newer and more popular.

Example of difference ksh and bash in condition test. First bash:

if [ $i -eq 3 ]

and condition test in ksh:

if (($i==3))

Bash can handle exit codes from pipes in a cleaner way. Bash and KSH are both Bourne=compatible shells, they share common functions and features and can be interchangeable to use.

Bash How to Read from Keyboard

Bash How to Read from Keyboard

To read input from the keyboard and assign input value to a variable use the read command.

Read syntax:

read options var1 var2

To write the value of first entered word use:

read var1 var2
echo $var1

If you don’t give any argument to the read command, input will assign to the environment variable REPLY.

The “s” option does not echo input while reading from a keyboard.

read -s -p "Enter password:" $password

The -p “TEXT” option displays TEXT to the user without a newline.

The -e option means that command readline is used to obtain the line.
Option -u FD reads inputs from file descriptor FD (0,1,2).
Option -t TIME causes that read returns a failure if the input is not read within TIME seconds.

Bash How to Return from Function

Bash How to Return from Function

Use the statement “return” to return from the function. You can also specify the return value. Example:

return 1234

We returned function status 1234 from function. Usually, we use 0 value for success and 1 for failure. It is similar to command exit which we use to terminate the script.

If you don’t use the return statement in the whole function, the status of the last executed command will be returned.

To verify returned status from the last called function use “$?”.

There is a difference between commands return and exit. The exit will cause the script to end at the line where it is called. Return will cause the current function to go out of scope and continue execution command after the function.

What is Bash Script

What is Bash Script

Metaphorically speaking bash script is like a ‘to-do list’. After you read the first entry you start realizing it. After you finished first entry, you continue with the second entry and so on.

A bash script is a text file that contains a mixture of commands.

Bash script can contain also functions, loops, conditional constructs. Scripts are commonly used for administration task like change file permission, creating disk backups. Using bash scripts is often faster than using the graphical user interface.

It’s important to mention that there is no difference between putting series of 10 commands into a script file and executing that script or you entering commands one by one to the command-line interface. In both situations, the result will be exactly the same thing.

After you create your script it is good practice to add the extension ‘.sh’ to the filename, for example ‘myFirstScript.sh’.

Bash Error Output Redirect

Bash Error Output Redirect

Each open file gets assigned a file descriptor. The file descriptors for STDIN is 0, for STDOUT is 1 nad STDERR is 2.

If you want to redirect just STDERR (standard error output) to file, ju do:

cmd_name 2> /file

If you want o redirect STDOUT to other files, and STDERR to other files, just do:

cmd_name >/stdout_file 2>/stderr_file

If you want to merge both (STDERR, STDOUT) into one file, you can do:

cmd_name >/file_4_both 2>&1

For the same effect, you can also use this syntax:

cmd_name &> /file_4_both

Even more, it is very useful to use the tee command. By definition, tee read from standard input and write to standard output and files, in same time.

In next example, we merge STDOUT and STDERR of “find /” command together. Then, we pass it to STDIN of tee command. Tee command is executed with -a parameter, that means append to an existing file (if exists):

find / 2>&1 | tee -a /home/tee.output
ll /home

Output: (lines omitted) -rw-r–r–. 1 root root 2.2M Jun 10 13:16 tee.output

The child process inherits open file descriptors. If you want to prevent file descriptors from being inherited, close it. For example:

<&-

This close stddin descriptor.

Bash How to do Math

Bash How to do Math

You can use arithmetic expansion with parentheses or square brackets:

a=0
echo $((a+5))

Output: 5

echo $[a+2]

Output: 2

Another option is using the command “bc”:

echo "5+3" | bc

Output: 8

If you need to do math with a float number, use the variable “scale” to define how operations use decimal numbers

echo "scale=2; 1/4" | bc

Output: 0.25

Evaluate expression with only integer you can use the command “expr”

echo `expr 10 - 2`

Output: 8

Be careful with multiply operations. Always escape * (asterisk) char with \. For example:

expr 5 \* 3

Output: 15

Bash How to Basename

Bash How to Basename

Command basename strip directory and suffix from filenames. Command syntax:

basename [option] name [suffix]

If the suffix is specified it will remove a trailing suffix. Example:

basename dir1/dir2/dir3/text_file.txt .txt

Output: text_file

Basename takes one argument (filename) and an optional suffix. If you want to give more file names use the option “a” which supports multiple arguments and threat each as “name”.

basename -a /dir/file.txt /dir2/picture.jpg

Output: file.txt picture.jpg

If you want to get the name of your home folder:

basename ~

Often used option is option -s which removes a trailing suffix. Here is an example:

basename -s .txt -a /dir/file.txt /dir2/picture.jpg

file picture.jpg

Bash Vs KSH

Bash Vs KSH

Linux and Unix have various shells. Two kinds of these numerous shells are KSH and BASH.

KSH (The Korn Shell) was developed many years before the BASH. Ksh has associative arrays and handles loop syntax better than bash. Also, ksh’s command print is better than bash’s echo command. In other way, ksh does not support history completion, process substitution, and rebindable command-line editing.

Bash has more added extension than ksh. Bash has tab completion and an easier method to set a prompt in order to display the current directory.

Compared to ksh, bash is newer and more popular.

Example of difference ksh and bash in condition test. First bash:

if [ $i -eq 3 ]

and condition test in ksh:

if (($i==3))

Bash can handle exit codes from pipes in a cleaner way. Bash and KSH are both Bourne=compatible shells, they share common functions and features and can be interchangeable to use.

Bash Scripting Best Practices

Bash Scripting Best Practices

Let’s begin with the first line of your script. The first rule always starts script with shebang. Without the shebang line, the system does not know which shell to process. For example:

#!/bin/bash

After shebang line write what is your script about and what it would do.

For debugging printout run your script with the “-x” or “-v” option like:

bash -x script.sh

Use the set lines:

set -e

set -u

set -o pipefail

“-e” to immediately exit if any command has non-zero exit status. “-u” causes the program to exit when you haven’t previously defined variable (except $* and $@). Option “-o pipefail” prevents fails in a pipeline from being masked. The exit status of the last command that threw non-zero exit code is returned.

Never use backticks, use:

$( ... )

Back-ticks are visually similar to single quotes, in a larger script with hundreds of lines you could be confused if it is back-ticks or single quotes.

If you need to create temporary files, use “mktemp” for temporary files and cleanup with “trap”.

Bash Hello World Script

Bash Hello World Script

This bash example creates an archive from /home directory to /backup/ directory as one tar.gz file. Let’s create a file backup.sh. It will consist of two lines:

#!/bin/bash
tar -czf /var/home-backup.tar.gz /home/

First line is a hashpling. Basically, it says who execute script. In this example, we choose /bin/bash.

The second line is tar command. It tarballs and compress the whole directory (/home) to one file.

I recommend you give also the third line (empty line). Why? If you execute this script in unusual UNIXes (ec. SCO UNIX), UNIX coudn’t execute last line, becasuse last symbol of file – EOF (end of file) is different from symbol EOLN (end of line). Symbol EOLN (enter) or semicolon (;) executes command.

Would you to extend this script to some output? Here is an example.

#!/bin/bash
echo -n Creating backup of home directory to /backup...
tar -czf /var/home-backup.tar.gz /home/ >/dev/null 2>&1
echo done.

On the second line, echo with n parameter doesn’t give a new line.

On the third line, output from tar command is redirected to trash (/dev/null).

Last line, just echoes done.

If you don’t know what to put on the first line (hashpling) type:

echo $SHELL

Output: /bin/bash

You will get path to your shell, which can use in the hashspling.

Bash How to Trim String

Bash How to Trim String

You can use a bash parameter expansion, sed, cut, and tr to trim a string.

Let’s use bash parameter expansion to remove all whitespace characters from the variable foo:

foo="Hello world."
echo "${foo//[["space:]]/}"

Output: Helloworld.

“${foo// /}” removes all space characters, “$foo/ /” removes the first space character.

To remove only space characters before and after string use sed:

foo=" Hello world. "
echo "${foo}" | sed -e 's/^[[:space:]]*//'

Output: Hello world.

Easy to use and remember the way how to remove whitespaces before and afterword:

echo " text. text2 " | xargs

xargs remove all whitespaces before “text.” and let one space between text. and text2.

If you want to delete only the last character from the variable:

foo="hello"
echo "${foo::-1}"

Output: hell