3 Terminal Tricks You’ll Wish You Knew Earlier

https://www.profitableratecpm.com/f4ffsdxe?key=39b1ebce72f3758345b2155c98e6709c

It doesn’t matter whether you learn the terminal or a seasoned pro, there are always a lot of little tips to learn – shutters that save you time. I have three useful bash tips that you can add to your arsenal.

Process substitution, to transform data into temporary data files

You have probably encountered a scenario where you needed a file path as a argument, but all you have are data.

For example, let’s say you want to modify a text that you already have in the terminal:

        echo "foo" | nano
    

The previous command does not work because you cannot kill the data in Nano; Instead, you must provide a file path, like this: nano /path/to/file.txt.

To solve this problem, you will probably use a sequence of commands similar to the following:

        echo foo > bar.txt
nano bar.txt

On two separate commands, you create a file, then do something with it. This approach is sub-optimal because it leaves the files broadcast on your file system, and you must also type the way. The substitution of the process solves this problem:

        echo <(true)
    

If you executed the previous command, you saw it printed a file path like what follows:

/dev/fd/16

The substitution of processes generates a temporary file from the output of an order, allowing another order to read it. Thus, in the previous example, “<(True)" generates the exit path, and "echo" receives it. The output data is simply "real".

The “FD” part of the path refers to the term “file descriptor”, which is only a identifier that software uses internally to designate the open files. This example has a file descriptor 16. File descriptors are deprived in the processes that use them – which is “echo” in this case.

Use a process substitution useful:

        nano <(echo "foo")
    

The previous command is the same as entering the following elements:

        echo "foo" > /tmp/foo
nano /tmp/foo

You will not be able to save and modify the text during the execution Nano <("Foo" echo) Because the specially created file is in reading alone, but you can change where you save it with the shortcut CTRL + O.

Substitution of the input / exit process

The expression “<(FOO)" creates a to input process substitution. Alternately, you can also create a to go out Substitution of the process:

        echo "foo" > >(cat)
    

The previous command is the same as entering the following elements:

        echo "foo" > /tmp/foo
cat /tmp/foo

Although the two commands can appear different, they work in the same way. It becomes clearer once you understand that “Foo” echo >> (cat) Written “Foo” in a special file, including the Cat bed then.

So, to summarize:

  • Echo <(True) is a to input Process substitution and Echo receives data via a special file path (in this case, the data is only “real”).
  • “Foo” echo >> (cat) is a to go out substitution of the process and CAT receives the word echo via a special path. The first “>” character is the redirect operator, and the second is part of the substitution expression itself.

To finish this section, I think that a real example will link it well:

        diff <(ls -1 ~) <(ls -1 /tmp)
    

This order will compare the differences between two directories. A substitution of input process is useful in this case because Diff is expecting file paths, and this is what process substitutions provide.

The “LS -1” flag is only one, not the letter l.

Expansion of history to repeat orders

Bash developers often like to use the term “expansion”. It seems complicated, but it just means changing a little thing in something bigger – to extend it. The most famous example is the expansion of Tilde (~), which develops in a path for your domestic repertoire.

The following three examples are all called extensions in history. These expressions all develop simple character inputs in the elements of the history of your shell.

Bang, Bang !!

This is one of my most used commands after CD and LS. It’s simple to use and understand. He reinstalls the last order you have executed.

        ls
!!
A terminal window displays the LS command executed twice. The second time, he was executed with a double exclamation point.

You can see in the order output that the LS command runs twice.

Develop a specific element in your history

First of all, look at your story to choose an appropriate command:

        history
    

Then simply choose one of the numbers on the left and insert it like N:

        !N
    
The terminal shows the story of Bash. A double exclamation point has entered and he re -executed the last order: a simple list of directories.

Check the command before re -executing it, because if you accidentally perform the poor order, you will have a bad day.

The best thing about the expansion of these numbered history elements is that their figures remain consistent, which allows you to familiarize yourself with them. Of course, this familiarity only lasts until you delete your ~ / .bash_history file.

Develop by name, for easier selection

The search for the history of your command often and the location of a specific numbered order can become tedious. An alternative and more efficient approach exists: BASH allows you to partially type the start of an order, and it will perform the most recent match.

        echo foo
!e
!ech

This is perfect during the alternation between several orders during a single terminal session. However, for long -term commands (much older), I prefer to use a blurred history search tool instead.

Command groups

Sometimes you will want to execute a certain number of group orders, but treat their release as a result. Consider the following example:

        echo foo > /tmp/foo.txt
echo bar >> /tmp/foo.txt

The “>” character is called the redirection operator, and it sends the control output to the specified file, crushing the content; “>>” do the same, except that he adds to the file – add Redirection operator.

The previous command will create a file that looks like:

foo
bar

Sometimes when your orders are more complex, the use of such an approach can be ugly due to many unnecessary lines. The simpler approach is to use a group of orders.

A group of orders merges all the outings of its orders in one. This makes treatment the result much simpler.

        { echo foo; echo bar; } > /tmp/foo.txt
    

This will result in the same outing as the previous command. Redirection should not occur once, so there is no use for the adding operator (>>).

A semicolon after each order is required, in particular the final order. Spaces between the supports and control words are also necessary.

You can prove that orders outside the group only run once by performing the following command sequence:

        { echo "A"; echo "B"; } | wc -l
    
A terminal window displays a group of controls with two echo instructions. The value is killed in the WC command which makes a number of lines, which displays number 2.

The group of orders sends the two Echo outings as a single flow. This is why “WC” has the two lines together as a single message and displays 2; Otherwise, it would display 1 on two separate lines.

An example of real world use cases:

        { ping -c 1 example.com; ping -c 1 example.net; } > /tmp/ping.txt

In summary:

  • The substitution of the process replaces a process with a temporary file path. For example, Nano <("Foo" echo) And “Foo” echo >> (cat).

  • The extensions of the shell history are special characters that refer to your Bash history. For example, ! foo,, ! N Or !!.

  • A group of orders unifies the outputs of several orders in a single result. For example, {Echo “Foo”; “bar” echo; }> /tmp/foo.txt.

All covered only scrapes the surface of what Bash has to offer. If you like the expansions in the story of Shell, you will love our tutorial on how to use the historical command. You can also be interested in tips that facilitate learning the command line.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button