Movatterモバイル変換


[0]ホーム

URL:


How-To Geek logo

8 Linux shell tricks that completely change how commands work

Tux, the Linux mascot, wearing sunglasses and peeking from behind a large terminal window displaying globbing commands.Credit: Lucas Gouveia/How-To Geek
4
By Bobby Jack
A technology enthusiast, Bobby studied Computer Science at the University of Southampton before working in a number of roles across industries, from the private sector to the charitable one, at multinationals and startups. He’s helped maintain backend Java servers, designed databases and front-end interfaces, and created a bespoke content management system.

Bobby also enjoys video gaming, and has written for several outlets, including a stint as Editor-in-Chief at Switch Player Magazine and contributions to online magazine,SUPERJUMP. Bobby uses a Mac for day-to-day work and an Android phone for distractions.
Sign in to yourHow-To Geek account
Summary
Jump links

Jump Links

follow
Follow
followed
Followed
Thread2
Here is a fact-based summary of the story contents:
Try something different:

The shell is the unsung hero of your Linux experience. Sitting between you and the programs your operating system runs, the shell is a user interface and a programming language rolled into one.

One of the shell’s responsibilities is to apply various expansions to the command you type, inserting variables, for example. Expansion involves several steps, though, so it’s worth understanding the process in full.

Brace expansion

The very first step is to take brace expressions and turn them into strings with an optional prefix and suffix. This is useful to generate longer lists of words based on a simple pattern. For example, the first type of brace expansion uses commas to separate alternatives:

echo a{b,c}de

Your shell will expand this toecho abde acde.

Using echo like this lets you see the final result after expansion.

The other type of brace expansion uses a sequence expression:

$ echo a.{1..5}.za.1.z a.2.z a.3.z a.4.z a.5.z

In this case, {1..5} is short for {1,2,3,4,5}. You can also use letters instead of numbers.

Tilde expansion

In its most basic form, the tilde character is a shortcut for your home directory, e.g.

$ echo ~/Users/bobby

There are more tilde expansions besides this, however, for example:

  • ~user expands to the path of that user’s home directory.
  • ~+ expands to thecurrent working directory (PWD).
  • ~- expands to the previous working directory (OLDPWD).

Parameter and variable expansion

To access a variable, use a $ followed by its name or, if it’s a positional parameter (used in shell scripts and functions), its number.

echo $PATHfoo() { echo $1 } && foo hello

In most cases, you should enclose the variable name with braces:

echo ${PATH}

This helps prevent a bunch of errors, and also prepares you to use one of the many advanced types of parameter expansion that Bash supports. For example, you can set a variable to a default value if it’s unset or null, using the:- syntax:

LOCAL_PATH=${PATH:-/bin}

You can also carry out basic string manipulation using substring expansion:

$ GREETING="Hello, world"$ echo ${GREETING:7}world

You can even convert a string to uppercase or get its length:

$ TITLE="lowercase"$ echo ${TITLE@U}LOWERCASE$ echo ${#TITLE}9

Bash supports many other types of parameter expansion, so it’s worth exploringits string manipulation features.

Command substitution

Command substitution is one of the most convenient upgrades you can make to your daily shell life, especially if you’re writing scripts. In simple terms, it looks like this:

echo $(ls)

Bash will run the command in a subshell, replacing the original $(...) with its output. This is often useful when the command outputs a filename. For example, the mktemp command creates a temporary file and outputs its name. So, to start editing a temporary file with a single command, you can run:

vi $(mktemp)

Or you can create and enter a temporary directory with:

cd $(mktemp -d)

Process substitution is another type of expansion that looks a bit like command substitution. However, it’s quite different from the others on this list, and isn’t available on every system.

Arithmetic expansion

You’ll probably find arithmetic is of most use in certain types of shell scripts. The trickiest thing about arithmetic expansion is remembering the double-bracket syntax:

$(( 11 * 42 ))

Other than that, you can carry out all basic integer arithmetic that you’d expect: multiplication, post-increment, and bitwise shifts, for example. You can also use standard comparison operators like == for equality and logical operators like && for AND.

Word splitting

At this point, the shell splits the results of previous expansions that did not occur between double quotes into words. It splits on characters from the IFS variable, which are typically space, tab, and newline.

Take this example:

FILES="one two three"ls ${FILES}

Bash will initially replace the second command with "ls one two three". Since that expansion was not double-quoted, word splitting will result in three parameters passed to ls: one, two, and three. If the expansion were quoted, however, it would be a different story:

FILES="one two three"ls "${FILES}"

In this case, ls will complain about a missing file named "one two three". This emphasizes the difference between three arguments separated by spaces and one single argument that contains spaces.

Filename expansion

Filename expansion, otherwise known as globbing, lets you match files using patterns. Even if you’ve never heard of globbing or filename expansion, you’ve probably used it before, like this:

ls *.txt

With this form of expansion, the shell looks for unquoted cases of the *, ?, and [ characters. If it finds one, it treats that word as a pattern and replaces it with all matching filenames.

While * matches any number of characters, ? matches a single character, and [...] matches a character from the given set.

Quote removal

Finally, quotes (and the backslash character, which can be used to escape them) are removed from any part of the original string before expansions.

This lets you quote arguments that you want treated as a single argument, even if they contain spaces. So, to list a problematically-named file:

ls "a filename with spaces"

Without quotes, ls will try to list four separate files with single-word names.

Follow
Followed
Share
FacebookXWhatsAppThreadsBlueskyLinkedInRedditFlipboardCopy linkEmail
Readers like you help support How-To Geek. When you make a purchase using links on our site, we may earn an affiliate commission.Read More.
A MacBook surrounded by a gear symbol, a shield, an iCloud icon, and a password dots bar.
I made my Mac more secure by changing these 5 settings
A Chromebook keyboard with the search button as the center focus.
These 5 Chromebook tips save me tons of time in Google Docs
Two Linux penguins, one cheerful with a 'Love' button, the other confused with a 'Hate' button.
5 reasons people give up on Linux (and why it’s time to come back)
See More
The back of the OnePlus 15 sitting in grass and leaves.
The OnePlus 15 can finally be sold in the U.S.
A replacement battery for a Kindle third generation eReader.
It’s time to admit you can swap out internal rechargeable batteries yourself
Several smartphones arranged diagonally on a blue geometric background, each displaying a simple home screen with a solid black wallpaper
Black is the new best wallpaper for your phone
See More

[8]ページ先頭

©2009-2025 Movatter.jp