How to Turn Off Echo While Executing a Shell Script Linux

How to turn off echo while executing a shell script Linux

You could use Bash redirection :

  • command 1> /.../path_to_file => to redirect stdout into path_to_file.

command > /.../path_to_file is a shortcut of the previous command.

  • command 2> /.../path_to_file => to redirect stderr into path_to_file

To do both at the same time to the same output: command >/.../path_to_file 2>&1.

2>&1 means redirect 2 (stderr) to 1 (stdout which became path_to_file).
You could replace path_to_file by /dev/null if you don't want to retrieve the output of your command.

Otherwise, you could also store the output of a command :

$ var=$(command) # Recent shell like Bash or KSH
$ var=`command` # POSIX compliant

In this example, the output of command will be stored in $var.

How do I turn off echo in a terminal?

stty_orig=`stty -g`
stty -echo
echo 'hidden section'
stty $stty_orig

How to echo shell commands as they are executed

set -x or set -o xtrace expands variables and prints a little + sign before the line.

set -v or set -o verbose does not expand the variables before printing.

Use set +x and set +v to turn off the above settings.

On the first line of the script, one can put #!/bin/sh -x (or -v) to have the same effect as set -x (or -v) later in the script.

The above also works with /bin/sh.

See the bash-hackers' wiki on set attributes, and on debugging.

$ cat shl
#!/bin/bash

DIR=/tmp/so
ls $DIR

$ bash -x shl
+ DIR=/tmp/so
+ ls /tmp/so
$

How to hide command output in Bash

Use this.

{
/your/first/command
/your/second/command
} &> /dev/null

Explanation

To eliminate output from commands, you have two options:

  • Close the output descriptor file, which keeps it from accepting any more input. That looks like this:

    your_command "Is anybody listening?" >&-

    Usually, output goes either to file descriptor 1 (stdout) or 2 (stderr). If you close a file descriptor, you'll have to do so for every numbered descriptor, as &> (below) is a special BASH syntax incompatible with >&-:

    /your/first/command >&- 2>&-

    Be careful to note the order: >&- closes stdout, which is what you want to do; &>- redirects stdout and stderr to a file named - (hyphen), which is not what what you want to do. It'll look the same at first, but the latter creates a stray file in your working directory. It's easy to remember: >&2 redirects stdout to descriptor 2 (stderr), >&3 redirects stdout to descriptor 3, and >&- redirects stdout to a dead end (i.e. it closes stdout).

    Also beware that some commands may not handle a closed file descriptor particularly well ("write error: Bad file descriptor"), which is why the better solution may be to...

  • Redirect output to /dev/null, which accepts all output and does nothing with it. It looks like this:

    your_command "Hello?" > /dev/null

    For output redirection to a file, you can direct both stdout and stderr to the same place very concisely, but only in bash:

    /your/first/command &> /dev/null

Finally, to do the same for a number of commands at once, surround the whole thing in curly braces. Bash treats this as a group of commands, aggregating the output file descriptors so you can redirect all at once. If you're familiar instead with subshells using ( command1; command2; ) syntax, you'll find the braces behave almost exactly the same way, except that unless you involve them in a pipe the braces will not create a subshell and thus will allow you to set variables inside.

{
/your/first/command
/your/second/command
} &> /dev/null

See the bash manual on redirections for more details, options, and syntax.

How to keep the internal shell script running while the main shell script ends?

You can put the process in the background with &:

#!/bin/bash

if pgrep infiloop > /dev/null ;
then
echo "Process is running."
else
exec /u/team/infiloop.sh > /u/team/infiloopOutput.txt &
echo "Process was not running, started process $!"
fi

How can I suppress all output from a command using Bash?

The following sends standard output to the null device (bit bucket).

scriptname >/dev/null

And if you also want error messages to be sent there, use one of (the first may not work in all shells):

scriptname &>/dev/null
scriptname >/dev/null 2>&1
scriptname >/dev/null 2>/dev/null

And, if you want to record the messages, but not see them, replace /dev/null with an actual file, such as:

scriptname &>scriptname.out

For completeness, under Windows cmd.exe (where "nul" is the equivalent of "/dev/null"), it is:

scriptname >nul 2>nul

Date is getting Printed in echo command while executing shell script

First of all, you should prefer $() in bash to fork a subshell.

To print out your desired command without being interpreted by bash, you need to escape $, i.e.

#!/bin/bash
echo "0 20 * * * touch /global/appaem/aem/wrap-lock/wrap.lock-\$(date +\"%Y%m%d\")" >> /tmp/tmp.txt


Related Topics



Leave a reply



Submit