622
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 ...
285
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 ...
265
votes
Accepted
How does a Segmentation Fault work under-the-hood?
All modern CPUs have the capacity to interrupt the currently-executing machine instruction. They save enough state (usually, but not always, on the stack) to make it possible to resume execution later,...
258
votes
How do I use pushd and popd commands?
There is a really useful use case for pushd and popdcommands for working with several folders simultaneously.
You can navigate the stack very easily, since it is enumerated.
Meaning, you can have ...
254
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 ...
226
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 ...
188
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 ...
174
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 ....
171
votes
Accepted
Search for a previous command with the prefix I just typed
What you are looking for is CtrlR.
Type CtrlR and then type part of the command you want. Bash will display the first matching command. Keep typing CtrlR and bash will cycle through previous ...
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 ...
158
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; ...
156
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 ...
131
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)...
125
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.
...
120
votes
How to determine where an environment variable came from?
Some places to look first:
System wide
/etc/environment: specifically meant for environment variables
/etc/env.d/*: environment variables, split in multiple files
/etc/profile: all types of ...
120
votes
Accepted
How to Count the Number of Lines of an Output?
You can pipe the output in to wc. You can use the -l flag to count lines. Run the program normally and use a pipe to redirect to wc.
python Calculate.py | wc -l
Alternatively, you can redirect the ...
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 ...
110
votes
Accepted
Use of touch and vi?
touching the file first confirms that you actually have the ability to create the file, rather than wasting time in an editor only to find out that the filesystem is read-only or some other problem.
105
votes
Accepted
shell find -delete "directory not empty"
As @Stephen Kitt mentions, this is largely a duplicate of find -delete does not delete non-empty directories which states that you're telling it to delete directories, but the directories aren't empty ...
102
votes
Accepted
mv: Move file only if destination does not exist
mv -vn file1 file2. This command will do what you want. You can skip -v if you want.
-v makes it verbose - mv will tell you that it moved file if it moves it(useful, since there is possibility that ...
100
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 ...
98
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 ...
96
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
Difference between Login Shell and Non-Login Shell?
To tell if you are in a login shell:
prompt> echo $0
-bash # "-" is the first character. Therefore, this is a login shell.
prompt> echo $0
bash # "-" is NOT the first character. This is NOT a ...
90
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 ...
87
votes
How to get the pid of the last executed command in shell script?
Get PID:
#!/bin/bash
my-app &
echo $!
Save PID in variable:
#!/bin/bash
my-app &
export APP_PID=$!
Save all instances PID in text file:
#!/bin/bash
my-app &
echo $! >>/tmp/my-...
85
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 ...
84
votes
Accepted
Is there any reason to have a shebang pointing at /bin/sh rather than /bin/bash?
There are systems not shipping bash by default (e.g. FreeBSD).
Even if bash is installed, it might not be located in /bin.
Most simple scripts don't require bash.
Using the POSIX shell is more ...
84
votes
Difference between 'ls' and 'echo $(ls)'
When you run this command:
ls
the terminal displays the output of ls.
When you run this command:
echo $(ls)
the shell captures the output of $(ls) and performs word splitting on it. With the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
shell × 11993bash × 4109
shell-script × 2388
linux × 1431
scripting × 728
command-line × 685
zsh × 518
quoting × 498
awk × 479
terminal × 444
sed × 423
ssh × 381
files × 375
text-processing × 374
io-redirection × 371
find × 366
grep × 347
pipe × 330
wildcards × 308
environment-variables × 270
variable × 246
ubuntu × 213
posix × 190
process × 185
filenames × 172