- represents all the arguments passed to a script or a function as separate, individual strings. It preserves each argument as a distinct item, even if the argument contains spaces.
*** key points about $@
1. All Arguments: It refers to all the command-line arguments passed to the script.
2. Preserves Spaces: If an argument contains spaces, it is treated as a single argument.
3. Usage: It is commonly used in a loop to iterate over each argument.
*** example
#+begin_src bash
./myscript.sh file1.txt "file with spaces.txt" file3.txt
#+end_src
then $@ will be
=file1.txt "file with spaces.txt" file3.txt=
*** differences between $*
- *$@* treats each argument as a sepearte item
- *$** treats all arguments as a single, combined string
** logical operators
- you can use the error return value in conditionals
*** example using the OR conditional
#+begin_src bash
false || echo "Oops fail"
"Oops fail"
#+end_src
*** example using the AND conditional
#+begin_src bash
true && echo "this will print"
"this will print"
#+end_src
** concatenate commands
- you caoncatinate commands using the semicolon: *;*
#+begin_src bash
false; echo "this prints always"
"this prints always"
#+end_src
** common substitution
- take the output of a command and put it in a variable
#+begin_src bash
foo=$(pwd)
echo $foo
"/Users/ronny/..."
#+end_src
- this can also be done by placing that format in a string and it will expand the string
#+begin_src bash
echo "the pwd output is: $(pwd)"
"the pwd output is: /Users/ronny/..."
#+end_src
** process substitution
*** description
Bash process substitution is a feature that allows you to use the output of a command or a process as if it were a file. It enables you to redirect input or output between processes in a flexible way without needing intermediate temporary files.
*** Syntax
- creates a temporary file descriptor for the output of the command, which can be used as an input file in another command.
#+begin_src bash
<(command)
#+end_src
- creates a temporary file descriptor for writing to the command, which can be used as an output file in another command.
#+begin_src bash
>(command)
#+end_src
*** examples
- this will take b, a, d, c and sort it so the result is 'a, b, c, d'
#+begin_src bash
sort <(echo -e "b\na") <(echo -e "d\nc")
#+end_src
- this will list the files, send the list to *tee* which will split a portion off to the screen and the rest to grep, followed by text_files.txt
#+begin_src bash
ls | tee >(grep "txt" > text_files.txt)
#+end_src
#+begin_src bash
echo "Starting program at $(date)" # Date will be substituted
echo "Running program $0 with $# arguments with pid $$"
for file in "$@"; do
grep foobar "$file" > /dev/null 2> /dev/null
# when pattern is not found, grep has exit status 1
# we redirect STDOUT and STDERR to a null register since we do not care about them
if [[ "$?" - ne 0 ]]; then
echo "File $file does not have any foober adding one"
echo "# foobar" >> "$file"
fi
done
#+end_src
- *$$* pid given for program
- *$#* number of arguments
- *$@* expands to all the arguments
- can be used in a for loop
- *2>* refers to STDERR
- *>* refers to STDOUT
** test utility
test is a bash utility that you can use to test the condition of a file. look at the man page for more info
** curly braces
curly braces are used as a form of program command expansion.
the braces contain a number of arguments seperated by commans that will expand into arguments for the program