644
votes
What is the difference between the Bash operators [[ vs [ vs ( vs ((?
In Bourne-like shells, an if statement typically looks like
if
command-list1
then
command-list2
else
command-list3
fi
The then clause is executed if the exit code of the command-list1 list ...
291
votes
Replacing only specific variables with envsubst
Per the man page:
envsubst [OPTION] [SHELL-FORMAT]
If a SHELL-FORMAT is given, only those environment variables that
are referenced in SHELL-FORMAT are substituted; otherwise all
environment ...
272
votes
Accepted
How can we run a command stored in a variable?
This has been discussed in a number of questions on unix.SE, I'll try to collect all issues I can come up with here. Below is
a description of why and how the various attempts fail,
a way to do it ...
253
votes
Accepted
Understanding the -exec option of `find`
This answer comes in the following parts:
Basic usage of -exec
Using -exec in combination with sh -c
Using -exec ... {} +
Using -execdir
Basic usage of -exec
The -exec option takes an external ...
200
votes
How to monitor CPU/memory usage of a single process?
Procpath
2020 update (Linux/procfs-only). Returning to the problem of process analysis frequently enough and not being satisfied with the solutions I described below originally, I decided to write my ...
177
votes
Accepted
What's the difference between eval and exec?
eval and exec are completely different beasts. (Apart from the fact that both will run commands, but so does everything you do in a shell.)
$ help exec
exec: exec [-cl] [-a name] [command [arguments ....
168
votes
Accepted
Why eval the output of ssh-agent?
ssh-agent outputs the environment variables you need to have to connect to it:
shadur@proteus:~$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-492P67qzMeGA/agent.7948; export SSH_AUTH_SOCK;
SSH_AGENT_PID=7949; ...
162
votes
What is the difference between the Bash operators [[ vs [ vs ( vs ((?
(…) parentheses indicate a subshell. What's inside them isn't an expression like in many other languages. It's a list of commands (just like outside parentheses). These commands are executed in a ...
158
votes
Accepted
What is the difference between "cat file | ./binary" and "./binary < file"?
In
./binary < file
binary's stdin is the file open in read-only mode. Note that bash doesn't read the file at all, it just opens it for reading on the file descriptor 0 (stdin) of the process it ...
133
votes
Accepted
How is this command legal? "> file1 < file2 cat"
Before the shell executes the cat command on the command line, it handles any redirections. Redirections include redirecting input or output using < (stdin, read-only), <> (stdin, read+write)...
127
votes
Accepted
What is the purpose of the "do" keyword in Bash for loops?
Note that that syntax is inherited from the Bourne shell.
After the variable name, you can have either in to have the list of elements explicitly given, or do, to loop over the positional parameters.
...
113
votes
How to pass parameters to an alias?
Alias solution
If you're really against using a function per se, you can use:
$ alias wrap_args='f(){ echo before "$@" after; unset -f f; }; f'
$ wrap_args x y z
before x y z after
You can replace ...
113
votes
How can I show a terminal shell's process tree including children?
Try the --forest (or -H) flag
# ps -ef --forest
root 114032 1170 0 Apr05 ? 00:00:00 \_ sshd: root@pts/4
root 114039 114032 0 Apr05 pts/4 00:00:00 | \_ -bash
root 56225 ...
107
votes
Accepted
Using "find" non-recursively?
You can do that with -maxdepth option:
/bin/find /root -maxdepth 1 -name '*.csv'
As mentioned in the comments, add -mindepth 1 to exclude starting points from the output. From man find:
-maxdepth ...
97
votes
Confusing use of && and || operators
Here's my cheat sheet:
"A ; B" Run A and then B, regardless of success of A
"A && B" Run B if A succeeded
"A || B" Run B if A failed
"A &" Run A in background.
94
votes
Accepted
Why avoid using "&&" in bash script?
The review comment probably refers to the second usage of the && operator. You don't want to not exit if the echo fails, I guess, so writing the commands on separate lines makes more sense:
if ...
91
votes
What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?
All of these mean something different, and you can write different things inside them (or the same things, with different meaning). Different kinds of quote interpret different escape sequences inside ...
89
votes
Accepted
Simple command-line calculator
You can reduce the amount of verbosity involved in using bc:
$ bc <<<"236-192"
44
$ bc <<<"1+1"
2
(assuming your shell supports that).
If you’d rather have that ...
87
votes
Accepted
How to make a for loop in command line?
The syntax of a for loop from the bash manual page is
for name [ [ in [ word ... ] ] ; ] do list ; done
The semicolons may be replaced with carriage returns, as noted elsewhere in the bash manual ...
86
votes
Where is the `--` (double dash) argument documented?
This is a POSIX requirement for all utilities, see POSIX chapter 12.02,
Guideline 10 for more information:
The first -- argument that is not an option-argument should be accepted as a delimiter ...
79
votes
Accepted
What's a good mnemonic for shell double vs. single quotes?
Single quotes are simple quotes, with a single standard: every character is literal.
Double quotes have a double standard: some characters are literal, others are still interpreted unless there's a ...
78
votes
Accepted
How can I set the default editor as nano on my Mac?
Set the EDITOR and VISUAL environment variables to nano.
If you use bash, this is easiest done by editing your ~/.bashrc file and adding the two following lines:
export EDITOR=nano
export VISUAL="...
73
votes
Accepted
Why a "login" shell over a "non-login" shell?
The idea is that a user should have (at most) one login shell per host.
(Perhaps I should say, one login shell per host per terminal —
if you are simultaneously logged in to a host through multiple ...
71
votes
Prepending a timestamp to each line of output from a command
For a line-by-line delta measurement, try gnomon.
It is a command line utility, a bit like moreutils's ts, to prepend timestamp information to the standard output of another command. Useful for long-...
70
votes
Why are POSIX mandatory utilities not built into the shell?
Why are POSIX mandatory utilities not built into shell?
Because to be POSIX compliant, a system is required1 to provide most utilities as standalone commands.
Having them builtin would imply they ...
69
votes
Why avoid using "&&" in bash script?
This is not a general comment against &&. I suspect the engineer you spoke to was considering the (very unlikely, but still theoretically possible) case where the echo itself failed. If you ...
68
votes
Accepted
what is $$ in bash?
From Advanced Bash-Scripting Guide:
$$ is the process ID (PID) of the script itself.
$BASHPID is the process ID of the current instance of Bash. This is not the same as the $$ variable, but ...
67
votes
Accepted
How to define a shell script to be sourced not run
Assuming that you are running bash, put the following code near the start of the script that you want to be sourced but not executed:
if [ "${BASH_SOURCE[0]}" -ef "$0" ]
then
echo "Hey, you ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
shell × 12160bash × 4163
shell-script × 2413
linux × 1447
scripting × 734
command-line × 697
zsh × 533
quoting × 499
awk × 487
terminal × 457
sed × 427
ssh × 383
text-processing × 380
files × 376
io-redirection × 375
find × 367
grep × 348
pipe × 332
wildcards × 310
environment-variables × 275
variable × 247
ubuntu × 218
posix × 195
process × 187
filenames × 173