Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script print commands, but do not print echo command

I would like to write a bash script the prints the commands. But for readability purposes, I do not want it to print the echo commands. Unfortunately, I cannot find the correct settings for my bash script to achieve this. I need help?

#!/bin/bash

# Makes the bash script to print out every command before it is executed
set -v

echo "Cleaning test database"
RAILS_ENV=test bundle exec rake db:drop
echo "************************************************************"
echo ""

echo "Setting up the test database"
RAILS_ENV=test bundle exec rake db:setup
echo "************************************************************"
echo ""

The output looks like:

echo "Cleaning test database"
Cleaning test database
RAILS_ENV=test bundle exec rake db:drop
echo "************************************************************"
************************************************************
echo ""


echo "Setting up the test database"
Setting up the test database
RAILS_ENV=test bundle exec rake db:setup

As you can see it prints out all the commands including the echo command, which I do not want to see.

like image 799
Gerardo Navarro Suarez Avatar asked Oct 29 '15 10:10

Gerardo Navarro Suarez


People also ask

How do you stop echo in Bash?

If you are looking to suppress or hide all the output of a bash shell script from Linux command line as well as from the crontab then you can simply redirect all the output to a file known as /dev/null . This file is known as Black Hole which will engulf everything you give without complaining.

What is the difference between echo and printf in Bash?

Printf provides for the creation of a formatting string and offers a non-zero quit status when it fails. Whereas echo normally leaves with a 0 status and typically outputs inputs headed by the end of line character upon this standard result. The “printf” gives you more options for the output format than the “echo”.

What is $@ Bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What does @echo do command?

echo command in linux is used to display line of text/string that are passed as an argument . This is a built in command that is mostly used in shell scripts and batch files to output status text to the screen or a file.


2 Answers

You could use trap DEBUG instead of set -v as one option.

For example

#!/bin/bash


# Makes the bash script to print out every command before it is executed except echo
trap '[[ $BASH_COMMAND != echo* ]] && echo $BASH_COMMAND' DEBUG

echo "Cleaning test database"
RAILS_ENV=test bundle exec rake db:drop
echo "************************************************************"
echo ""

echo "Setting up the test database"
RAILS_ENV=test bundle exec rake db:setup
echo "************************************************************"
echo ""

Debug is executed after every command.
$BASH_COMMAND is currently running command.

BASH_COMMAND The command currently being executed or about to be executed, unless the shell is executing a command as the result of a trap, in which case it is the command executing at the time of the trap.

So the trap just checks if the last command did not start with echo and prints it.

like image 156
123 Avatar answered Oct 17 '22 07:10

123


Thanks @123 !

To filter multiple strings from the DEBUG trap (not just echo commands), we can use regexp match too.

For example, to filter out any command that starts with "echo" or "read" or "if", I use:

trap '! [[ "$BASH_COMMAND" =~ ^(echo|read|if) ]] && echo $PS4$BASH_COMMAND' DEBUG

Update:

In order to show bash ${variable} values expended, I now use:

trap '! [[ "$BASH_COMMAND" =~ ^(echo|read|if) ]] && \
cmd=`eval echo "$BASH_COMMAND" 2>/dev/null` && echo "$cmd"' DEBUG
like image 6
Noam Manos Avatar answered Oct 17 '22 07:10

Noam Manos