Today I Learned
-
A common construct an bash scripts is an
if
block like so:if [ -z "$STRING" ]; then
echo "Length of string is zero"
fiThe syntax seems similar to what you find in many other languages. For example
if (string.length) {
console.log('Length of string is zero');
}But if you've tried using a bash
if
block, you may have noticed that you'll get an error if you omit the space between the brackets and their content:if [-z "$STRING" ]; then
echo "Length of string is zero"
fi
[-z: command not foundThis seemed like Bash was just being a bit picky about the formatting here. But today I learned this isn't so.
In fact,
[
is essentially just an alias for thetest
command: it's not just for grouping things. So, when you run[-z "$STRING" ]
you are actually running
test-z "$STRING"
And of course,
test-z
is not a command, so it produces an error. -
You can resize images at the command line with Image Magick. First, install it:
sudo apt install imagemagick
Then convert:
convert original.png -resize 100x100 new.png
Works great! For more basic use cases, see this article at Digital Ocean.
-
Bash aliases only work at the beginning of the command. But this doesn't mean they only work at the beginning of the command line. For example, given the alias
alias clip="xclip -sel clipboard"
It works fine when piping.
$ someCommand | clip
Now the output of
someCommand
is loaded to your clipboard. -
The
xargs
command can be used to feed the output of one program as variadic arguments to another program.For example, here's how you can delete all Git branches that match the glob
feat/*
:git branch --list 'feat/*' | xargs git branch -D
-
To kill a process by name, rather than job number or PID, you can use
pkill
. For example, this will kill all open VSCode instances.pkill code
Today I accidentally turned on the Redshift program, and it was flickering annoyingly. I started to look for a solution online, but then remembered:
pkill redshift
Problem solved. Use this power wisely.
-
Easy access of command history
Instead of using up and down to cycle through terminal history, you can use Ctrl + p and Ctrl+n, for slightly better typing ergonomics.
-
Create new files without touching them
Instead of creating new files with
touch
, you can use the redirection operator:# this works
$ touch foo.txt
# but this is easier
$ > foo.txtThe redirection operator is also more generally capable, because you can add text to the file that you are creating.
echo node_modules > .gitignore
-
Insert all completions
You can use Alt + * to insert all completions at the shell. For instance, if you type
rm foo
followed by tab, and there are multiple files starting withfoo
in the CWD, then you'll see a list of these files. If you want to remove all of them, type Alt + * to insert them all as arguments torm
. -
Clean up before killing
When killing a process, it's usually better to not use the
-9
flag. The commandkill -9
immediately kills a process, without giving it time to clean up after itself. Instead, try usingkill -15
first. This allows a process to terminate itself gracefully.kill -9
is better reserved as a command of last resort.