150 votes

How to exit a shell script if one part of it fails?

One approach would be to add set -e to the beginning of your script. That means (from help set): -e Exit immediately if a command exits with a non-zero status. So if any of your commands fail, ...
terdon's user avatar
  • 238k
132 votes
Accepted

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

Multiple arguments in shebang

Although not exactly portable, starting with coreutils 8.30 and according to its documentation you will be able to use: #!/usr/bin/env -S command arg1 arg2 ... So given: $ cat test.sh #!/usr/bin/...
unode's user avatar
  • 1,380
76 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 ...
Gilles 'SO- stop being evil''s user avatar
66 votes
Accepted

Bash multiplication and addition

Using arithmetic expansion: for (( k = 0; k < 50; ++k )); do a=$(( 2*k + 1 )) echo "$a" done Using the antiquated expr utility: for (( k = 0; k < 50; ++k )); do a=$( expr 2 '*' "$k" + 1 ...
Kusalananda's user avatar
  • 327k
65 votes

Bash shell script output alignment

Use printf to format output (it's also more portable than echo). I would also store the real value of the colour escape sequences instead of storing them in a form that requires expansion by echo. RED=...
Stéphane Chazelas's user avatar
63 votes
Accepted

How do I find the line number in Bash when an error occured?

Rather than use your function, I'd use this method instead: $ cat yael.bash #!/bin/bash set -eE -o functrace file1=f1 file2=f2 file3=f3 file4=f4 failure() { local lineno=$1 local msg=$2 echo ...
slm's user avatar
  • 366k
56 votes

Bash shell script output alignment

Simply with column command: yourscript.sh | column -t The output: Network 10.x.xx.xxx : Online Network 10.x.xx.xxx : Offline Network 10.x.xx.xxx : Offline Network 10.x.xx.xxx ...
RomanPerekhrest's user avatar
54 votes
Accepted

How do I replace AND (&&) in a for loop?

The equivalent to your original sequence would be: for i in {1..20} do cmd $i || break done The difference with Amit's answer is the script won't exit, i.e. will execute potential commands that ...
jlliagre's user avatar
  • 60.7k
54 votes
Accepted

How to run ssh command until succeeded?

Another option would be to use until. until ssh [email protected]; do sleep 5 done If you do this repeatedly for a number of hosts, put it in a function in your ~/.bashrc. repeat() { read -p "...
Hunter.S.Thompson's user avatar
51 votes
Accepted

Replace character X with character Y in a string with bash

So many ways, here are a few: $ string="a,b,c,d,e" $ echo "${string//,/$'\n'}" ## Shell parameter expansion a b c d e $ tr ',' '\n' <<<"$string" ## With "tr" a b c d e $ sed 's/,/\n/g' &...
heemayl's user avatar
  • 55.5k
47 votes
Accepted

How to use less in a script without getting ESC escape characters?

Use -R flag: -r or --raw-control-chars Causes "raw" control characters to be displayed. The default is to display control characters using the caret notation; for example, a control-A (...
Vombat's user avatar
  • 12.7k
47 votes
Accepted

At sign after shebang?

This looks like a placeholder in an GNU Automake template which is going to be filled in by a configure script. So it's neither a Perl or Unix kernel thing, but a GNU autotools thing. It is probably ...
Kusalananda's user avatar
  • 327k
47 votes
Accepted

Multiple arguments in shebang

There is no general solution, at least not if you need to support Linux, because the Linux kernel treats everything following the first “word” in the shebang line as a single argument. I’m not sure ...
Stephen Kitt's user avatar
47 votes

Using Python in a Bash Script

To run a set of Python commands from a bash script, you must give the Python interpreter the commands to run, either from a file (Python script) that you create in the script, as in #!/bin/bash -e # ...
Kusalananda's user avatar
  • 327k
47 votes

How to run a script every 64 hours?

Just run every hour and check that the number of hours since some arbitrary instant (for instance, instant 0 of the Unix epoch time) is a multiple of 64: 0 * * * * t=$(date +\%s); [ "$(( (t / ...
Stéphane Chazelas's user avatar
45 votes

How can I suppress output only if the command succeeds?

moreutils' chronic command does just that: chronic mycommand will swallow mycommand's output, unless it fails, in which case the output is displayed.
Stephen Kitt's user avatar
45 votes
Accepted

One-liner to generate an easily memorable password?

First of all, install a dictionary of a language you're familiar with, using: sudo apt-get install <language-package> To see all available packages: apt-cache search wordlist | grep ^w Note: ...
meeDamian's user avatar
  • 582
44 votes

How to grep for same string but multiple files at the same time?

If it is simpler, you can just specify each file one after the other. grep -E 'fatal|error|critical|failure|warning' file1.log file2.log
flickerfly's user avatar
  • 1,641
44 votes
Accepted

How to check if the current time is between 23:00 and 06:30

If all you need is to check if HH:MM is between 23:00 and 06:30, then don't use Unix timestamps. Just check the HH:MM values directly: fireup() { while :; do currenttime=$(date +%H:%M) if [[...
muru's user avatar
  • 71.3k
42 votes

tmux script to launch several commands

tmux \ new-session 'compass watch /path/to/project1/compass/' \; \ split-window 'compass watch /path/to/project2/compass/' \; \ detach-client The new-session command (which creates a new ...
Kusalananda's user avatar
  • 327k
41 votes

how to implement logrotate in shell script

how about using savelog? It's available in debian and RH and pretty much every other linux distro I know of. It's a /bin/sh shell script, so should run on any other unix too. e.g. before writing ...
cas's user avatar
  • 77.7k
41 votes

How to run ssh command until succeeded?

ssh [email protected] until !!; do sleep 5 ; done The !! to repeat the last command.
GAD3R's user avatar
  • 64.3k
40 votes
Accepted

get `top` output for non-interactive shell

top -b -n 1 From man top: -b : Batch mode operation Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In ...
joepd's user avatar
  • 2,367
40 votes
Accepted

reason for exec in wrapper scripts

Using exec makes the wrapper more transparent, i.e. it makes it less likely that the user or application that calls the script needs to be aware that it's a relay that in turns launches the “real” ...
Gilles 'SO- stop being evil''s user avatar
40 votes
Accepted

Execute command in sftp connection through script

You can change your script to pass commands in a here-document, e.g., #!/bin/bash sftp -oPort=23 [email protected]:/home/kalenpw/TestWorld/plugins <<EOF put /home/kalenpw/.m2/repository/...
Thomas Dickey's user avatar
40 votes
Accepted

Linux shell script: Run a program only if it exists, ignore it if it does not exist

My interpretation would use a wrapper function named the same as the tool; in that function, execute the real tool if it exists: figlet() { if command -p figlet >/dev/null 2>&1 then ...
Jeff Schaller's user avatar
  • 66.7k
40 votes
Accepted

Does Linux support invoking a program directly via its inode number?

The short answer is no. The longer answer is that linux user API doesn't support accessing files by any method using the inode number. The only access to the inode number is typically through the ...
user10489's user avatar
  • 6,253

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