diff options
author | EuAndreh <eu@euandre.org> | 2024-11-18 08:21:58 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2024-11-18 08:44:57 -0300 |
commit | 960e4410f76801356ebd42801c914b2910a302a7 (patch) | |
tree | 615d379416f72956d0c1666c63ce062859041fbe /src/content/tils/2020/11/12/useful-bashvars.adoc | |
parent | Remove jekyll infrastructure setup (diff) | |
download | euandre.org-960e4410f76801356ebd42801c914b2910a302a7.tar.gz euandre.org-960e4410f76801356ebd42801c914b2910a302a7.tar.xz |
Diffstat (limited to 'src/content/tils/2020/11/12/useful-bashvars.adoc')
-rw-r--r-- | src/content/tils/2020/11/12/useful-bashvars.adoc | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/content/tils/2020/11/12/useful-bashvars.adoc b/src/content/tils/2020/11/12/useful-bashvars.adoc new file mode 100644 index 0000000..33a072e --- /dev/null +++ b/src/content/tils/2020/11/12/useful-bashvars.adoc @@ -0,0 +1,72 @@ +--- + +title: Useful Bash variables + +date: 2020-11-12 1 + +layout: post + +lang: en + +ref: useful-bash-variables + +eu_categories: shell + +--- + +[GNU Bash][gnu-bash] has a few two letter variables that may be useful when +typing on the terminal. + +[gnu-bash]: https://www.gnu.org/software/bash/ + +## `!!`: the text of the last command + +The [`!!` variable][previous-command] refers to the previous command, and I find +useful when following chains for symlinks: + +[previous-command]: https://www.gnu.org/software/bash/manual/bash.html#Event-Designators + +```shell +$ which git +/run/current-system/sw/bin/git +$ readlink $(!!) +readlink $(which git) +/nix/store/5bgr1xpm4m0r72h9049jbbhagxdyrnyb-git-2.28.0/bin/git +``` + +It is also useful when you forget to prefix `sudo` to a command that requires +it: + +```shell +$ requires-sudo.sh +requires-sudo.sh: Permission denied +$ sudo !! +sudo ./requires-sudo.sh +# all good +``` + +Bash prints the command expansion before executing it, so it is better for you +to follow along what it is doing. + +## `$_`: most recent parameter + +The [`$_` variable][recent-parameter] will give you the most recent parameter +you provided to a previous argument, which can save you typing sometimes: + +```shell +# instead of... +$ mkdir -p a/b/c/d/ +$ cd a/b/c/d/ + +# ...you can: +$ mkdir -p a/b/c/d/ +$ cd $_ +``` + +[recent-parameter]: https://www.gnu.org/software/bash/manual/bash.html#Special-Parameters + +## Conclusion + +I wouldn't use those in a script, as it would make the script terser to read, I +find those useful shortcut that are handy when writing at the interactive +terminal. |