- How do I turn off echo in a terminal?
- 4 Answers 4
- How can I suppress all output from a command using Bash?
- 9 Answers 9
- How to enable or disable multiple «echo statements» in bash ecript
- 8 Answers 8
- Enable/Disable via CLI Parameter
- Linux echo command
- Description
- Syntax
- Options
- Options
- Options
- Escape sequences
- Examples
- Related commands
How do I turn off echo in a terminal?
Obviously, I don’t want the password being echoed to the terminal, so I want to turn off echo for the duration of the read. I know there’s way to do this with stty , but I’ll ask the question for the benefit of the community whilst I go read the manpage. 😉
4 Answers 4
stty_orig=`stty -g` stty -echo echo 'hidden section' stty $stty_orig
This is certainly the way I’ve done it before, if memory serves. Just out of curiosity, is this any different from stty -echo; read foo; stty echo ?
More general, better than Wikipedia when you’re new to unix @AbhishekGupta: 1. learn how to scroll, search, and quit less (or more ). 2. man whateveryouwonderabout or, for bash builtin commands such as «set», help set . Good luck on your journey 😉
read -s password works on my linux box.
This is useful information but -s seems to be a Bash extension, as read -s foo produces read: 1: Illegal option -s in Dash (the Bourne shell which Ubuntu symlinks to /bin/sh ).
You can use ‘-s’ option of read command to hide user input.
echo -n "Password:" read -s password if [ $password != ". " ] then exit 1; # exit as password mismatched # fi
Also you can use ‘stty -echo’ if you want to hide from terminal to print. And restore the terminal settings using «stty echo»
But I think for getting password input from user ‘read -s password’ is more than enough.
Bourne Shell Script :
#!/bin/sh # Prompt user for Password echo -n 'Password: ' # Do not show what is being typed in console by user stty -echo # Get input from user and assign input to variable password read password # Show what is being typed in console stty echo
stty manual command for more information:
stty manual snippets:
STTY(1) stty 5.2.1 (March 2004) STTY(1) NAME stty - change and print terminal line settings SYNOPSIS stty [-F DEVICE] [--file=DEVICE] [SETTING]. stty [-F DEVICE] [--file=DEVICE] [-a|--all] stty [-F DEVICE] [--file=DEVICE] [-g|--save] DESCRIPTION Print or change terminal characteristics. -a, --all print all current settings in human-readable form -g, --save print all current settings in a stty-readable form -F, --file=DEVICE open and use the specified DEVICE instead of stdin --help display this help and exit --version output version information and exit Optional - before SETTING indicates negation. An * marks non-POSIX settings. The underlying system defines which settings are available. Local settings: [-]echo echo input characters
How can I suppress all output from a command using Bash?
I have a Bash script that runs a program with parameters. That program outputs some status (doing this, doing that. ). There isn’t any option for this program to be quiet. How can I prevent the script from displaying anything? I am looking for something like Windows’ «echo off».
9 Answers 9
The following sends standard output to the null device (bit bucket).
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:
For completeness, under Windows cmd.exe (where «nul» is the equivalent of «/dev/null»), it is:
Note that ‘&>’ is peculiar to bash (and maybe C shells); Korn and Bourne shells require 2> /dev/null or 2>&1 (to send stderr to the same place as stdout). The Korn shell seems to interpret ‘&>’ as «run the stuff up to & in background, then do the i/o redirection on an empty command».
Note that some commands write directly to the terminal device rather than to standard output/error. To test, put echo foo > $(tty) in a script and run ./test.sh &> /dev/null — The output is still printed to the terminal. Of course this is not a problem if you’re writing a script which uses no such commands.
@l0b0, is there a way to make the script’s (or any other program’s) tty different so that all output is redirected regardless of your example? I know screen and script can do something like that but both are finicky and vary from *nix to *nix.
This will prevent standard output and error output, redirecting them both to /dev/null .
Even though I’m adding this to my gs (GhostScript) command, it still prints out **** Warning: File has a corrupted %%EOF marker, or garbage after %%EOF. Any advice?
An alternative that may fit in some situations is to assign the result of a command to a variable:
$ DUMMY=$( grep root /etc/passwd 2>&1 ) $ echo $? 0 $ DUMMY=$( grep r00t /etc/passwd 2>&1 ) $ echo $? 1
Since Bash and other POSIX commandline interpreters does not consider variable assignments as a command, the present command’s return code is respected.
Note: assignement with the typeset or declare keyword is considered as a command, so the evaluated return code in case is the assignement itself and not the command executed in the sub-shell:
$ declare DUMMY=$( grep r00t /etc/passwd 2>&1 ) $ echo $? 0
How to enable or disable multiple «echo statements» in bash ecript
I have bash script where i have echo before every command showing what is happening. But i need to disbale echo when setting as cron job and then enable again if do some testing. i find it very hard to go to each line and then add/remove comment is there anything which i can include at top something like
enable echo or disable echo
8 Answers 8
The absolute easiest would be to insert the following line after the hashbang line:
When you want to re-enable, either delete the line or comment it out:
If you’re not using echo but printf , same strategy, i.e.:
If you absolutely need to actually echo/printf something, prepend the builtin statement, e.g.:
builtin echo "This 'echo' will not be suppressed."
This means that you can do a conditional output, e.g.:
Set the SOME_KIND_OF_FLAG variable to something non-null, and the overridden echo function will behave like normal echo .
EDIT: another alternative would be to use echo for instrumenting (debugging), and printf for the outputs (e.g., for piping purposes). That way, no need for any FLAG . Just disable/enable the echo() < :; >line according to whether you want to instrument or not, respectively.
Enable/Disable via CLI Parameter
Put these lines right after the hashbang line:
if [[ debug == "$1" ]]; then INSTRUMENTING=yes # any non-null will do shift fi echo ()
Now, invoking the script like this: script.sh debug will turn on instrumenting. And because there’s the shift command, you can still feed parameters. E.g.:
- Without instrumenting: script.sh param1 param2
- With instrumenting: script.sh debug param1 param2
The above can be simplified to:
if [[ debug != "$1" ]]; then echo () < :; >shift fi
if you need the instrumenting flag (e.g. to record the output of a command to a temp file only if debugging), use an else -block:
if [[ debug != "$1" ]]; then echo () < :; >shift else INSTRUMENTING=yes fi
REMEMBER: in non-debug mode, all echo commands are disabled; you have to either use builtin echo or printf . I recommend the latter.
Linux echo command
On Unix-like operating systems, the echo command prints text to standard output, e.g., the terminal.
This page covers the GNU/Linux version of echo.
Description
echo is a fundamental command found in most operating systems. It is frequently used in scripts, batch files, and as part of individual commands; anywhere you may need to output text.
Most command shells, including bash, ksh and csh implement echo as a built-in command. The behavior of built-in echo commands is similar, but the options may be different; those commands are not documented here.
This page covers the stand-alone program, /bin/echo. Its options are slightly different than the built-in echo command that is included in your shell. If you are using the bash shell, you can determine which echo is the default, using the type command:
To specify that you want to run the stand-alone program instead of the shell built-in, use its complete path in the command, i.e., run it like this:
This page describes the GNU/Linux stand-alone version of echo.
Syntax
echo [SHORT-OPTION]. [STRING].
Options
Options
These options may be specified before the string, and affect the behavior of echo.
-n | Do not output a trailing newline. |
-e | Enable interpretation of backslash escape sequences (see below for a list of these). |
-E | Disable interpretation of backslash escape sequences. This is the default. |
Options
If a long option is specified, you may not specify a string to be echoed. These options are for getting information about the program only.
—help | Display a help message and exit. |
—version | Output version information and exit. |
Escape sequences
If you specify the -e option, the following escape sequences are recognized in your string:
Sequence | Interpreted as |
---|---|
\\ | A literal backslash character («\«). |
\a | An alert (The BELL character). |
\b | Backspace . |
\c | Produce no further output after this. |
\e | The escape character; equivalent to pressing Esc . |
\f | A form feed. |
\n | A newline. |
\r | A carriage return. |
\t | A horizontal tab. |
\v | A vertical tab. |
\0NNN | Byte with octal value NNN (which can be 1 to 3 digits). |
\xHH | Byte with hexadecimal value HH (which can be either 1 or 2 digits) |
Each shell generally has its own implementation of echo, which may be slightly different than the version described here. Refer to your shell’s documentation for details about the options it supports.
Examples
In the above command, the two words (Hello, and world!) are passed to echo as separate arguments, and echo prints them in sequence, separated by a space:
The next command produces the same output:
However, unlike the first example, the above command provides the single-quoted string ‘Hello, world!‘ as a single argument.
Single-quoting a string will reliably protect it from interpretation by the shell, passing special characters and escape sequences literally to echo.
For instance, in the bash shell, variable names are preceded by a dollar sign ($). In the next command, the variable name inside the quotes is treated literally; outside the quotes, it is converted to its value.
/bin/echo 'The value of $PATH is' $PATH
The value of $PATH is /home/hope/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Escape sequences are not interpreted, by default:
/bin/echo 'Here, \bthe \bbackspace \bsequences \bare \bignored.'
Here, \bthe \bbackspace \bsequences \bare \bignored.
However, if you provide the -e option, they are interpreted:
/bin/echo -e 'Here, \bhowever, \bthe \bbackspace \bsequences \bare \binterpreted.'
Here,however,thebackspacesequencesareinterpreted.
If you need to insert newlines in your echo output, specify the -e option and include the \n escape sequence wherever you want a new line:
echo -e 'Here,\nwe\nhave\ninserted\nnewlines.'
Here, we have inserted newlines.
echo -e 'Here\twe\thave\tinserted\thorizontal\ttabs.'
Here we have inserted horizontal tabs.
echo -e 'This line is not completely \cprinted.'
This line is not completely
Related commands
cat — Output the contents of a file.
printf — Write formatted output.
tac — Output the contents of files in reverse order.
tee — Route a file’s contents to multiple outputs.
touch — Update the timestamp of a file or directory.
tr — Translate one set of characters to another.