In my opinion, every developer should customize their command line environment to be as efficient as possible! Like other languages, you can use existing libraries and commands to automate frequent tasks. Here are my go to commands that I use as tools within other scripts.
1. TheCommand
Command
This command is super useful for determining when a command exists. Thecommand
command is similar to thewhich
command, however, you can ignore the output when a command is not found. Since the path of a command only displays when a particular command exists, thecommand
command is great for if statements in your script.
if ! command -v MyCommand; then echo "MyCommand could not be found" exitfi
2. Thexargs
Command
This command is a lot like the methodmap
in popular languages like JavaScript, Python, or Ruby. This command allows devs to run another command for each space, tab, newline, or EOF (you can change the delimiter). This is my go to command when parsing lists or csv files.
# get line count for each php filels *.php | xargs wc -l
3. Thefind
Command
This command is likels
on steroids. It can list out folder contents but it can also walk a file tree. This is super useful if you want to find a file with a specific name but you don't know where it is. This command could be a whole other article because it has advance querying capabilities.
find $PWD -name "*.php"
4. Thecurl
Command
This command is all you need for fetching remote data through common protocols like FTP, HTTP/S, LDAP, MQTT, SMB, Telnet, etc. As an average Joe, I tend to use curl for consuming REST APIs. For example, you could get JSON from GitHub's or Reddit's public API.
curl -s https://api.github.com/repos/forem/forem/commits\?per_page\=5
5. Thejq
Command (Requires Install)
This is a command line utility built specifically for parsing JSON. It can index JSON objects/arrays, and it can also handle advanced queries, optional chaining, conditionals, and more. If you are consuming JSON in your bash scripts, this is a must have.
# get a list of unique commit authors from GitHub repocurl -s "https://api.github.com/repos/forem/forem/commits?per_page=20" \ | jq -r '[.[].commit.author.name] | unique | .[]'
6. Bonus: Theman
Command
Most well engineered commands have a--help
flag but sometimes that flag only shows you which arguments are accepted. If you want to get a full overview of how a command works and sometimes "why it works" you should use theman
command. This command will show you a specific manual page for a command. Yes, some commands have multiple pages.
man findman 3 echoman 7 hostname
Conclusion / takeaways
Bash scripting can be overwhelming but once you break down your scripts into components which can be handled by other tools, it becomes a very powerful language for getting things done.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse