447 votes
Accepted

How can I get the current working directory?

There's no need to do that, it's already in a variable: $ echo "$PWD" /home/terdon The PWD variable is defined by POSIX and will work on all POSIX-compliant shells: PWD Set by the shell ...
user avatar
  • 219k
145 votes
Accepted

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 ...
user avatar
  • 73.9k
141 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, ...
user avatar
  • 219k
140 votes
Accepted

Easy command line method to determine specific ARM architecture string?

On Debian and derivatives, dpkg --print-architecture will output the primary architecture of the machine it’s run on. This will be armhf on a machine running 32-bit ARM Debian or Ubuntu (or a ...
user avatar
130 votes

Is there a unix command that gives the minimum/maximum of two numbers?

If you know you are dealing with two integers a and b, then these simple shell arithmetic expansions using the ternary operator are sufficient to give the numerical max: $(( a > b ? a : b )) and ...
user avatar
117 votes
Accepted

How to get whole command line from a process?

You could use the -o switch to specify your output format: $ ps -eo args From the man page: Command with all its arguments as a string. Modifications to the arguments may be shown. [...] You may ...
user avatar
100 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, ...
user avatar
  • 68.6k
93 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.
user avatar
  • 1,141
92 votes

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

grep -E 'fatal|error|critical|failure|warning|' *.log
user avatar
  • 4,480
91 votes
Accepted

Copy image from clipboard to file

You can actually do this with xclip using -t option. See what targets are available: $ xclip -selection clipboard -t TARGETS -o TARGETS image/png text/html Note the image/png target; go ahead and ...
user avatar
  • 103k
86 votes
Accepted

$@ except the 1st argument

First, note that $@ without quotes makes no sense and should not be used. $@ should only be used quoted ("$@") and in list contexts. for i in "$@" qualifies as a list context, but here, to loop over ...
user avatar
77 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 ...
user avatar
  • 1,568
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 ...
user avatar
71 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/...
user avatar
  • 1,140
62 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. ...
user avatar
61 votes
Accepted

Add path to $PATH if not already in $PATH

First check if the path to add is already part of the variable: [[ ":$PATH:" != *":/path/to/add:"* ]] && PATH="/path/to/add:${PATH}" If /path/to/add is already in the $PATH, then nothing ...
user avatar
  • 45.8k
58 votes
Accepted

Using the setuid bit properly

The setuid bit can be set on an executable file so that when run, the program will have the privileges of the owner of the file instead of the real user, if they are different. This is the difference ...
user avatar
56 votes

Automated ssh-keygen without passphrase, how?

The simplest way I found to do what you want is this (example using default filename) < /dev/zero ssh-keygen -q -N "" If the ~/.ssh/id_rsa file already exists, the command will exit ...
user avatar
  • 711
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 ...
user avatar
  • 58.3k
52 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 ...
user avatar
  • 276k
51 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 ...
user avatar
  • 348k
50 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 ...
user avatar
50 votes
Accepted

How to run ssh command until succeeded?

Another option would be to use until. until ssh me@device.local; 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 "...
user avatar
48 votes
Accepted

How do I extract the content of quoted strings from the output of a command?

Using grep + sed This will parse the contents of those 2 strings: $ grep -o '".*"' somefile | sed 's/"//g' arch arch2 The above looks for a string matching the pattern ".*". That will match ...
user avatar
  • 348k
47 votes

How can I get the current working directory?

dir=$(pwd) This is more portable and preferred over the backticks method. Using $() allow you to nest the commands eg : mech_pwd=$(pwd; echo in $(hostname))
user avatar
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 ...
user avatar
  • 276k
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 / ...
user avatar
46 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 ...
user avatar
46 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 # ...
user avatar
  • 276k
45 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' &...
user avatar
  • 51.3k

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