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.

May 052013
 

Move forward or backward by a word in bash shell

Answer:

To move more efficiently in bash shell, you can use the following short cuts

Meta + f (move forward by a word)
Meta + b (move backward by a word)

The Meta key is the Alt key under most systems, on Mac, you would need to enable "use option as meta key" in the Terminal's Preference.

Jun 292012
 

How to ignore error in Bash script

Answer:

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 $?
0

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

Answer:

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

#!/bin/bash

if [ -e test.txt ]
then
  echo 'The file exists.'
else
  echo 'The file does not exist.'
fi