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.

Multi-line strings in Bash

Answer:

Bash support multiple line string, e.g.

#!/bin/bash
sort  <<EOT
apple
orange
banna
EOT

When you execute the script,

# bash test.sh

It gives:

apple
banna
orange

How to perform syntax check on a bash script?

Answer:

You can perform syntax check on a bash script, without actually running it using the following command:

# bash -n script.sh

But if your script contain execution of other program, bash will not try to run it, even if it does not exist, error of this type will not be returned.

How to avoid command being saved to shell history

Answer:

Firstly, you need to set the Bash shell environment variable to tell the shell to ignore the history if the command is starting with a particular character, e.g. a space in our case below.

# export HISTIGNORE="&:[ ]*"

Then, type the command start with a space

# who

And verify using the history command

# history

Moving around with Bash short-cut

Answer:

Bash has some shortcuts that allow you to move around the command prompt

1. Ctrl + a

Just to the start of the current line.

2. Ctrl + e

Just to the endof the current line.

Repeat previous command in Bash

Answer:

It is easy to type !! when you want to repeat the previous executed command in Bash

# ls -l /var/log/messages
-rw-r----- 1 syslog adm 330 Mar 29 17:17 /var/log/messages

# !!
ls -l /var/log/messages
-rw-r----- 1 syslog adm 330 Mar 29 17:17 /var/log/messages