Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Jun 292012

How to ignore error in Bash script


We have learned in the past on how to show the exit status of a given command.

But is it possible ignore the error and return normal exit code even the command has failed?

Yes, pipe "|| true" to the end of command, e.g.

# foo || true
-bash: foo: command not found
# echo $?

As you can see even the command foo was not found, our last exit code is still zero.

Aug 232011

Check if a file exist in Bash Shell


The following script demonstrates how to check if a file exist, using shell (Bash) script


if [ -e test.txt ]
  echo 'The file exists.'
  echo 'The file does not exist.'
Jun 032011

How to echo string to standard error (stderr)?


To echo string to the standard error (stderr), rather than the standard output (stdout), you can define the following function in your shell (put in your ~/.bashrc file)

# function echo2() { echo "$@" 1>&2; }

Then you can execute the command like:

# echo2 test

Jun 012011

Simple for loop in Bash shell


A simple for loop in Bash shell would like the following. You can use it wisely to skip a lot of repetitive tasks.


for i in {1..10}
   echo "I am command No. $i"