![]() |
|
More About Angle Brackets in Bash - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Computers (https://sickgaming.net/forum-86.html) +--- Forum: Linux, BSD & Unix (https://sickgaming.net/forum-88.html) +--- Thread: More About Angle Brackets in Bash (/thread-88454.html) |
More About Angle Brackets in Bash - xSicKxBot - 01-30-2019 More About Angle Brackets in Bash In the previous article, we introduced the subject of angle brackets ( Code: < >You can use Code: <Let’s say you are not sure your backup is complete, and you want to check that a certain directory contains all the files copied over from the original. You can try this: Code: diff <(ls /original/dir/) <(ls /backup/dir/)Code: diffCode: lsNote that there is no space between the Code: <Code: (...)Running that on the original and backup of a directory where I save pretty pictures, I get: Code: diff <(ls /My/Pictures/) <(ls /My/backup/Pictures/) 5d4 < Dv7bIIeUUAAD1Fc.jpg:large.jpgThe Code: <Code: diffSo, you may be wondering, if you can take the output of a command or command line, make it look like the contents of a file, and feed it to an instruction that is expecting a file, that means that in the sorting by favorite actor example from above, you could’ve done away with the intermediate file and just piped the output from the loop into Code: sortIn short, yep! The line: Code: sort -r <(while read -r name surname films;do echo $films $name $surname ; done < CBactors)does the trick nicely. Here string! Good string! There is one more case for redirecting data using angle brackets (or arrows, or whatever you want to call them). You may be familiar with the practice of passing variables to commands using Code: echoCode: |Code: myvar="Hello World" echo $myvar | tr '[:lower:]' '[:upper:]' HELLO WORLDThe Code: trCode: trIt is important to know that you are not passing on the variable, but only its contents, that is, the string “Hello World“. This is called the here string, as in “it is here, in this context, that we know what string we are dealing with“. But there is shorter, clearer, and all round better way of delivering here strings to commands. Using Code: tr '[:lower:]' '[:upper:]' <<< $myvardoes the same thing with no need to use echo or a pipe. It also uses angle brackets, which is the whole obsessive point of this article. Conclusion Again, Bash proves to give you lots of options with very little. I mean, who would’ve thunk that you could do so much with two simple characters like Code: <Code: >The thing is we aren’t done. There are plenty of more characters that bring meaning to chains of Bash instructions. Without some background, they can make shell commands look like gibberish. Hopefully, post by post, we can help you decipher them. Until next time! |