Skip to content

Latest commit

 

History

History
70 lines (55 loc) · 2.11 KB

08222022.md

File metadata and controls

70 lines (55 loc) · 2.11 KB
  1. NOTE: Linux system.
  • env - Run a program in a modified environment.

  • Fascinating trick with cd: these 2 commands below are totally equivalent.

    cd -
    cd $OLDPWD
  • Dash "-" command line parameters: Most of the commands of Linux treat the string "-" as a synonym for stdin or stdout.

    • For instance, when use cat command with naked -, it will read from the stdin:

      $ cat -
      Input text here and will be duplicated below. # ---> read from `stdin`.
      Input text here and will be duplicated below. # ---> `stdout` of `cat` command.
    • The - in the command above is actually an alias for /dev/stdin. Therefore, we can replace the dash with /dev/stdin.

      $ cat /dev/stdin
      Input text here and will be duplicated below. # ---> read from `stdin`.
      Input text here and will be duplicated below. # ---> `stdout` of `cat` command.
    • What is /dev/stdin? The answer is: /dev/stdin is a symbolic link to /proc/self/fd/0. The /proc/self/fd/0 also is a symbolic link to the standard input or our current shell process, which turn out to be our terminal. For that reason, we're able to input text to the cat command using our terminal.

    • Like stdin, when - is used in the context of stdout, it’s an alias for /dev/stdout:

      curl -L https://github.com/hishamhm/htop/archive/refs/tags/2.2.0.tar.gz --output -
    • In the case of git, we can use it as an argument to the git checkout command to check out the previous active branch or detached HEAD:

      $ git branch -a
      *master
      dev
      $ git checkout dev
      $ git branch -a
      master
      *dev
      $ git checkout -
      $ git branch -a
      *master
      dev
    • We can also use - as an argument to the cd command to switch between the current and the previous directory:

      $ pwd
      /home/hey/github
      $ cd TestApp
      $ pwd
      /home/hey/github/TestApp
      $ cd -
      $ pwd
      /home/hey/github
      $ cd -
      $ pwd
      /home/hey/github/TestApp