Quick linux tip

Linux never ceases to amaze me. I’ve been using it daily for years and somehow I haven’t come across this gem until now.

If you surround your terminal command with parentheses () then the command will execute and then return you back to your original directory.

Below I’ve got a really simple example. I’m in the folder “brackets” and I’ve created a folder “test”. I then use my parentheses around the command which moves into the test directory, creates a new file and lists the directory contents (showing the new file). Whenever the command finishes, I’m back in the folder I started in.

Linux terminal parentheses

Surrounding Linux terminal commands with brackets

Comments

  1. The reason it returns you to your original directory is because the parentheses create a sub-shell. This means that any changes made to the environment state such as variable modifications, configuration settings and such will revert once the parent shell process is returned to. As you’ve pointed out, that includes the current shell process working directory; note that this is kept track of within the $PWD environment variable by bash 4.x (and possibly other shells/versions.)

    My point is that many other bindings may be temporary utilized for processing a custom local environment that revert back to their original values once the child shell process has terminated. For example, function definitions and shell options as managed with the `shopt` builtin command are other settings that exhibit behavior similar to the file system location handling demonstrated in the original post.

Leave a Reply

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