Bash Commands

Commands of the Unix Shell, terminal (ls, man, redirection/pipeline operators). Explanation of ls -long output! (type of file, permissions). https://www.youtube.com/watch?v=tPKdfbL1Zv4

12 flashcards · Programming Languages · by lakesare

  1. printf VS echo?

    printf is a little smarter echo, eg it turns "\n" into line breaks.

  2. $ ls -l | grep Sep | less What's going on here?

    This is a pipeline operator, it feeds an output of process1 as input to process2. List files in the current directory (ls), retain only the lines of ls output containing the string "key" (grep), and view the result in a scrolling page (less), Pipes are unidirectional; data flows through the pipeline from left to right. Links: pipeline on wikipedia

  3. How to output files in long format: drwx------+ 14 lakesare staff 476 Sep 23 18:01 Desktop

    ls -l Links: man

  4. How to see instructions for, e.g., echo?

    man echo

  5. Rewrite command using pipeline operators: $ command1 | command2 to commands using redirection operator.

    command1 > tempfile command2 < tempfile rm tempfile The end result will be the same, however command2 won't start executing before command1 finished (with pipeline operator they can be run is parallel).

  6. What is the syntax for a redirection operator (<, >)?

    command redirection_operator file command1 > file1 command1 output writes into file1. command1 < file1 command1 is executed using file1 as input.

  7. Where will Unix search for an executable hello? $ hello

    In the $PATH (nonrecursively!), with the first matching executable being run. The /bin, /usr/bin, and /usr/local/bin directories are typically included in most users' $PATH setting (although this varies from implementation to implementation). ❯ echo $PATH /usr/local/opt/mysql@5.5/bin:/usr/local/opt/mysql@5.5/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/Applications/Wireshark.app/Contents/MacOS:/usr/local/mysql/bin:/Users/lakesare/Library/Python/3.6/b…

  8. What does brew link do?

    It symlinks the installed package (located in /usr/local/Cellar) to /usr/local. So when you type for example: $ <name-of-binary> in your terminal, then the package installed via brew is run.

  9. What does the first letter mean?

    It tells us what type of a file it is: - (file), d (directory), l (for Link)

  10. Explain permissions column.

    (Starting from the second letter) - rwxr-xr-x There are 3 permission columns here, each stands for permissions: (rwx) owner has over the directory (r-x) group has over the directory (r - x) - everybody has over the directory Where: r - read w - write x - execute https://www.garron.me/en/go2linux/ls-file-permissions.html

  11. hi=10 How to reference a variable in shell scripts?

    "${hi}" E.g.: if [ "${hi}" -gt 0 ]; then echo "YES"; else echo "NO"; So, first rule: always quote your variables. You can use either "$foo" or "${foo}" but quote it either way. Using quotes around a variable as in rm -- "$VAR1" or rm -- "${VAR}" is a good idea. This makes the contents of the variable an atomic unit. https://unix.stackexchange.com/a/4900/78176 When you use $foo or ${foo}, the shell will split the value saved in the variable on whitespace

  12. What is the difference between ' and " in shell scripts?

    Single quotes won't interpolate anything, but double quotes will. #!/bin/sh MYVAR=sometext echo "double quotes gives you $MYVAR" echo 'single quotes gives you $MYVAR' will give this: double quotes gives you sometext single quotes gives you $MYVAR