{"id":9897,"date":"2023-05-16T16:26:15","date_gmt":"2023-05-16T15:26:15","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=9897"},"modified":"2023-05-23T16:14:33","modified_gmt":"2023-05-23T15:14:33","slug":"streamlining-your-terminal-commands-with-custom-bash-functions-and-aliases","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2023\/05\/streamlining-your-terminal-commands-with-custom-bash-functions-and-aliases\/","title":{"rendered":"Streamlining Your Terminal Commands With Custom Bash Functions and Aliases"},"content":{"rendered":"\n<p>If you&#8217;ve ever found yourself typing out the same long commands over and over again, or if you&#8217;ve ever wished you could teleport directly to your favourite directories, then this post is for you.<\/p>\n\n\n\n<p>Before we jump into some useful examples, let&#8217;s go over what bash functions and aliases are, and how to set them up.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bash Functions vs Aliases<\/h2>\n\n\n\n<p>A <strong>bash function<\/strong> is like a mini script stored in your <code>.bashrc<\/code> or <code>.bash_profile<\/code> file. It can accept arguments, execute a series of commands, and even return a value.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>On the other hand, an <strong>alias<\/strong> is a simple shortcut for a command or a series of commands. It doesn&#8217;t handle arguments or complex logic like a bash function, but it&#8217;s great for quick and simple shortcuts.<\/p>\n\n\n\n<p>Here&#8217;s an example of a bash function:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">hello() {\n    echo \"Hello, $1\"\n}<\/pre>\n\n\n\n<p>And here&#8217;s an example of an alias:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">alias ll='ls -l'<\/pre>\n\n\n<a class=\"wp-block-read-more\" href=\"https:\/\/www.blopig.com\/blog\/2023\/05\/streamlining-your-terminal-commands-with-custom-bash-functions-and-aliases\/\" target=\"_self\">Read more<span class=\"screen-reader-text\">: Streamlining Your Terminal Commands With Custom Bash Functions and Aliases<\/span><\/a>\n\n\n<p>To add a custom function or alias, simply add it to your ~\/.bashrc file, or ~\/.zshrc if using zsh. Don&#8217;t forget to source the file after updating it!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Streamlining SLURM Commands<\/h2>\n\n\n\n<p>Working with SLURM commands can be a bit cumbersome, particularly if you&#8217;re running similar jobs repeatedly. Here&#8217;s where bash functions come into play. Let&#8217;s take a look at a couple of examples:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">slurm_run() {\n        srun --clusters=all -p \"$1\"-debug -D \"\/data\/localhost\/not-backed-up\/turnbull\" --pty \/bin\/bash\n}<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">slurm_run_gpu() {\n        srun --clusters=all --gpus 1 -p \"$1\"-debug -D \"\/data\/localhost\/not-backed-up\/turnbull\" --pty \/bin\/bash\n}<\/pre>\n\n\n\n<p>These functions let you quickly run interactive jobs on slurm, by simply typing slurm_run or slurm_run_gpu followed by the partition name, saving a lot of typing out and remembering of parameters!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Directory Access with Aliases<\/h2>\n\n\n\n<p>You might have certain directories that you access frequently. Typing out the full path every time can be a pain. Aliases are a great solution to this &#8211; here are a couple examples:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">alias data=\"cd \/vols\/opig\/users\/turnbull\/Data\"\nalias projects=\"cd \/data\/localhost\/turnbull\/Projects\"<\/pre>\n\n\n\n<p>Now, to go to your Data directory, you simply type <code>data<\/code>. To go to your Projects directory, just type <code>projects<\/code>. It&#8217;s as simple as that!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Monitoring SLURM Jobs<\/h2>\n\n\n\n<p>If you&#8217;re running a lot of jobs, it&#8217;s helpful to keep an eye on their status. Here&#8217;s an alias for that:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">alias watch_slurm=\"watch squeue -u turnbull --clusters=all\"<\/pre>\n\n\n\n<p>Typing <code>watch_slurm<\/code> will now keep a live update of your SLURM jobs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Custom bash functions and aliases are powerful tools that can significantly streamline your work in the terminal. They not only save time but also make your commands more readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve ever found yourself typing out the same long commands over and over again, or if you&#8217;ve ever wished you could teleport directly to your favourite directories, then this post is for you. Before we jump into some useful examples, let&#8217;s go over what bash functions and aliases are, and how to set them [&hellip;]<\/p>\n","protected":false},"author":82,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[348,29,296],"tags":[344,412],"ppma_author":[667],"class_list":["post-9897","post","type-post","status-publish","format-standard","hentry","category-bash","category-code","category-hints-and-tips","tag-bash","tag-how-to"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":667,"user_id":82,"is_guest":0,"slug":"olivert","display_name":"Oliver Turnbull","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/0b20bd3396ebbc89e8634a762722722d9168249244342ce2a0f4f6d05ed796eb?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/9897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/82"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=9897"}],"version-history":[{"count":3,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/9897\/revisions"}],"predecessor-version":[{"id":9912,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/9897\/revisions\/9912"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=9897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=9897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=9897"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=9897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}