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. The Command
Command
This command is super useful for determining when a command exists. The command
command is similar to the which
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, the command
command is great for if statements in your script.
if ! command -v MyCommand; then
echo "MyCommand could not be found"
exit
fi
2. The xargs
Command
This command is a lot like the method map
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 file
ls *.php | xargs wc -l
3. The find
Command
This command is like ls
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. The curl
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. The jq
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 repo
curl -s "https://api.github.com/repos/forem/forem/commits?per_page=20" \
| jq -r '[.[].commit.author.name] | unique | .[]'
6. Bonus: The man
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 the man
command. This command will show you a specific manual page for a command. Yes, some commands have multiple pages.
man find
man 3 echo
man 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)