Hot answers tagged

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 ...
John1024's user avatar
  • 74.1k
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 ...
don_crissti's user avatar
  • 81.4k
265 votes

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,...
zwol's user avatar
  • 7,127
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 ...
Jun Murakami's user avatar
  • 2,681
254 votes

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 ...
ilkkachu's user avatar
  • 135k
226 votes

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 ...
Kusalananda's user avatar
  • 327k
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 ...
saaj's user avatar
  • 2,308
174 votes

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 ....
ilkkachu's user avatar
  • 135k
171 votes

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 ...
John1024's user avatar
  • 74.1k
158 votes

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 ...
Stéphane Chazelas's user avatar
158 votes

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; ...
Shadur's user avatar
  • 30.9k
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 ...
Gilles 'SO- stop being evil''s user avatar
131 votes

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)...
Kusalananda's user avatar
  • 327k
125 votes

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. ...
Stéphane Chazelas's user avatar
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 ...
beetstra's user avatar
  • 1,301
120 votes

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 | wc -l Alternatively, you can redirect the ...
cutrightjm's user avatar
  • 5,100
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 ...
Tom Hale's user avatar
  • 29.5k
110 votes

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.
DopeGhoti's user avatar
  • 74.7k
105 votes

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 ...
Jason Rush's user avatar
  • 1,848
102 votes

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 ...
MatthewRock's user avatar
  • 6,906
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 ...
GILBERTO LINS's user avatar
98 votes

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 ...
Sundeep's user avatar
  • 11.9k
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.
user177073's user avatar
  • 1,171
94 votes

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 ...
choroba's user avatar
  • 46.4k
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 ...
Timothy Pulliam's user avatar
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 ...
Michael Homer's user avatar
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-...
Eduardo Cuomo's user avatar
85 votes

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 ...
user4556274's user avatar
  • 8,915
84 votes

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 ...
Marco's user avatar
  • 33.3k
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 ...
John1024's user avatar
  • 74.1k

Only top scored, non community-wiki answers of a minimum length are eligible