As of May 31, 2023, we have updated our Code of Conduct.
449 votes

How do I trim leading and trailing whitespace from each line of some output?

awk '{$1=$1;print}' or shorter: awk '{$1=$1};1' Would trim leading and trailing space or tab characters1 and also squeeze sequences of tabs and spaces into a single space. That works because when ...
Stéphane Chazelas's user avatar
378 votes

The "proper" way to test if a service is running in a script

systemctl has an is-active subcommand for this: systemctl is-active --quiet service will exit with status zero if service is active, non-zero otherwise, making it ideal for scripts: systemctl is-...
Stephen Kitt's user avatar
371 votes

Parallelize a Bash FOR Loop

Sample task task(){ sleep 0.5; echo "$1"; } Sequential runs for thing in a b c d e f g; do task "$thing" done Parallel runs for thing in a b c d e f g; do task "$thing" & done ...
Petr Skocik's user avatar
  • 27.8k
246 votes

In a bash script, using the conditional "or" in an "if" statement

The accepted answer is good but since you're using bash I'll add the bash solution: if [[ "$fname" == "a.txt" || "$fname" == "c.txt" ]]; then This is ...
jesse_b's user avatar
  • 35.4k
188 votes

How can I get my external IP address in a shell script?

NOTE: This is about external IP address (the one that the servers on the Internet see when you connect to them) - if you want internal IP address (the one that your own computer is using for ...
rsp's user avatar
  • 3,326
185 votes

Why is pattern "command || true" useful?

The reason for this pattern is that maintainer scripts in Debian packages tend to start with set -e, which causes the shell to exit as soon as any command (strictly speaking, pipeline, list or ...
Stephen Kitt's user avatar
165 votes

Understanding "IFS= read -r line"

In POSIX shells, read, without any option doesn't read a line, it reads words from a (possibly backslash-continued) line, where words are $IFS delimited and backslash can be used to escape the ...
Stéphane Chazelas's user avatar
160 votes

Replace environment variables in a file with their actual values?

You could use envsubst (part of gnu gettext): envsubst < infile will replace the environment variables in your file with their corresponding value. The variable names must consist solely of ...
don_crissti's user avatar
  • 78.4k
145 votes

Are there naming conventions for variables in shell scripts?

Yes, there are full code style conventions for bash, including variable names. For example, here's Google's Shell Style Guide. As a summary for the variable names specifically: Variable Names: ...
Anonsage's user avatar
  • 1,559
138 votes

How can I pass a command line argument into a shell script?

On a bash script, I personally like to use the following script to set parameters: #!/bin/bash helpFunction() { echo "" echo "Usage: $0 -a parameterA -b parameterB -c parameterC" echo -e "\...
Rafael Muynarsk's user avatar
137 votes

How to add/remove an element to/from the array in bash?

To add an element to the beginning of an array use. arr=("new_element" "${arr[@]}") Generally, you would do. arr=("new_element1" "new_element2" "..." "new_elementN" "${arr[@]}") To add an element ...
αғsнιη's user avatar
  • 40.7k
123 votes

Joining bash arguments into single string with spaces

I believe that this does what you want. It will put all the arguments in one string, separated by spaces, with single quotes around all: str="'$*'" $* produces all the scripts arguments separated ...
John1024's user avatar
  • 72.9k
122 votes

Why does the following script delete itself?

The kernel interprets the line starting with #! and uses it to run the script, passing in the script's name; so this ends up running /bin/rm scriptname which deletes the script. (As Stéphane ...
Stephen Kitt's user avatar
122 votes

What does "set --" do in this Dockerfile entrypoint?

The set command (when not setting options) sets the positional parameters eg $ set a b c $ echo $1 a $ echo $2 b $ echo $3 c The -- is the standard "don't treat anything following this as an option" ...
Stephen Harris's user avatar
118 votes

Bash throws error, line 8: $1: unbound variable

set -u will abort exactly as you describe if you reference a variable which has not been set. You are invoking your script with no arguments, so get_percent is being invoked with no arguments, ...
DopeGhoti's user avatar
  • 72.9k
115 votes

Shell Syntax: How to correctly use \ to break lines?

If the statement would be correct without continuation, you need to use \. Therefore, the following works without a backslash, as you can't end a command with a &&: echo 1 && echo 2 ...
choroba's user avatar
  • 45.3k
110 votes

Delete the last character of a string using string manipulation in shell script

Using sed it should be as fast as sed 's/.$//' Your single echo is then echo ljk | sed 's/.$//'. Using this, the 1-line string could be any size.
1111161171159459134's user avatar
106 votes

Bash: run command2 if command1 fails

The pseudo-code in the question does not correspond to the title of the question. If anybody needs to actually know how to run command 2 if command 1 fails, this is a simple explanation: cmd1 || ...
Orlando Rivera Letelier's user avatar
105 votes

`Syntax error: "(" unexpected` when creating an array

When you use ./ it executes with /bin/bash as in the first line with #!. But when you use sh it executes sh, not bash. The sh shell has no syntax to create arrays, but Bash ...
Konstantin Morenko's user avatar
101 votes

How do I remove the newline from the last line in a file in order to add text to that line?

If all you want to do is add text to the last line, it's very easy with sed. Replace $ (pattern matching at the end of the line) by the text you want to add, only on lines in the range $ (which means ...
Gilles 'SO- stop being evil''s user avatar
98 votes

How to add line numbers in every line using shell command?

The right tool for this job is nl: nl -w2 -s'> ' file You may want to tune width option according to the total number of lines in the file (if you want numbers to be aligned nicely). Output: 1&...
jimmij's user avatar
  • 45.7k
96 votes

Using grep in conditional statement in bash

You're almost there. Just omit the exclamation mark: OUTPUT='blah blah (Status: 200)' if echo "$OUTPUT" | grep -q "(Status:\s200)"; then echo "MATCH" fi Result: MATCH The if condition is ...
Guido's user avatar
  • 3,984
96 votes

Possible to match multiple conditions in one case statement?

You can use the ;;& conjunction. From man bash: Using ;;& in place of ;; causes the shell to test the next pattern list in the statement, if any, and execute any associated ...
steeldriver's user avatar
  • 77.5k
90 votes

How to get HOME, given USER?

There is a utility which will lookup user information regardless of whether that information is stored in local files such as /etc/passwd or in LDAP or some other method. It's called getent. In order ...
Jenny D's user avatar
  • 13k
89 votes

Passing named arguments to shell scripts

This is not a parser for positioned arguments, is for key=value aa=bb arguments; for ARGUMENT in "$@" do KEY=$(echo $ARGUMENT | cut -f1 -d=) KEY_LENGTH=${#KEY} VALUE="${...
JRichardsz's user avatar
88 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
85 votes

bash loop through list of strings

Using arrays in bash can aid readability: this array syntax allows arbitrary whitespace between words. strings=( string1 string2 "string with spaces" stringN ) for i in "${strings[@]}"...
glenn jackman's user avatar
82 votes

redirecting to /dev/null

To understand "redirecting to /dev/null" easily, write it out explicitly. Below is an example command that tries to remove a non-existent file (to simulate an error). rm nonexisting.txt 1>...
Thyag's user avatar
  • 941
82 votes

Multiple logical operators, ((A || B) && C), and "syntax error near unexpected token"

The syntax of bash is not C-like, even if a little part of it is inspired by C. You can't simply try to write C code and expect it to work. The main point of a shell is to run commands. The open-...
Gilles 'SO- stop being evil''s user avatar
82 votes

The "proper" way to test if a service is running in a script

systemctl does have a mode suitable for scripting; use show rather than status, and add the -p / --properties and --value options to get only the output you want. Here's an example (from an Ubuntu 17....
Zanna's user avatar
  • 3,471

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