{"id":1528,"date":"2017-01-14T11:52:42","date_gmt":"2017-01-14T11:52:42","guid":{"rendered":"http:\/\/cdblog.cdstealer.com\/?p=1528"},"modified":"2024-12-08T08:20:58","modified_gmt":"2024-12-08T08:20:58","slug":"bash-shell-tips","status":"publish","type":"post","link":"https:\/\/cdblog.cdstealer.com\/?p=1528","title":{"rendered":"Bash Shell Tips"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-425\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2011\/06\/gnometerminal.png\" alt=\"\" width=\"256\" height=\"256\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2011\/06\/gnometerminal.png 256w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2011\/06\/gnometerminal-150x150.png 150w\" sizes=\"auto, (max-width: 256px) 100vw, 256px\" \/><\/p>\n<p>Just a few things I find helpful :)<\/p>\n<hr \/>\n<h6>Bash history with timestamp.<\/h6>\n<p>This is more of an audit thing, but sometimes it's useful to know when you did something.<\/p>\n<p>If you type the command <em><strong>history<\/strong><\/em> into your shell. \u00a0You get back a list of the last X number of command executed.<\/p>\n<pre>$ history | tail -n1\n502 history | tail -n1<\/pre>\n<p>But by adding the following to your ~\/.bashrc (local user) or \/etc\/bash\/bashrc (all users) file, you can inject the time\/date. \u00a0Please see\u00a0<em><strong>man date<\/strong><\/em> for the options you can use.<\/p>\n<pre>HISTTIMEFORMAT='%F %T '<\/pre>\n<p>Example<\/p>\n<pre>$ history | tail -n1\n508 2017-01-14 11:29:58 history | tail -n1<\/pre>\n<hr \/>\n<h6>Bash History Duplicate Removal<\/h6>\n<p>It's an annoyance when you execute a command consecutively many time and then have to search further back to get to the last different command. \u00a0Behold! \u00a0Add this to your .bashrc(local user) or \/etc\/bash\/bashrc (all users) and no matter how many times you execute that command consecutively, it will only store the one time.<\/p>\n<pre>HISTCONTROL=ignoreboth<\/pre>\n<p>Example<\/p>\n<pre>user@server ~ $ vi .bashrc\nuser@server ~ $ vi .bashrc\nuser@server ~ $ vi .bashrc\nuser@server ~ $ vi .bashrc\nuser@server ~ $ history | tail -n5\n508 2017-01-14 11:29:58 history | tail -n1\n509 2017-01-14 11:31:36 man date\n510 2017-01-14 11:32:43 history\n511 2017-01-14 11:40:52 vi .bashrc\n512 \u00a02017-01-14 11:42:36 history | tail -n5<\/pre>\n<hr \/>\n<h6>Double tap exit\/logoff<\/h6>\n<p>Accidentally logging out of a shell session or user session can be annoying. \u00a0But there is hope :) \u00a0Add this to your .bashrc(local user) or \/etc\/bash\/bashrc (all users) and now you have to double tap to get out.<\/p>\n<p>This one ideally needs to go into the \/etc\/bash\/bashrc or else it would only work when closing your own shell session.<\/p>\n<pre>IGNOREEOF=1<\/pre>\n<p>Example<\/p>\n<pre>server ~ # Use \"logout\" to leave the shell.\nserver ~ # logout\nuser@server ~ $<\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Extract Path or File<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ export VAR=\/home\/me\/mydir\/file.c<br><br>$ echo \"${VAR%\/*}\"<br>\/home\/me\/mydir<br><br>$ echo \"${VAR##*\/}\"<br>file.c<\/pre>\n\n\n\n<h6 class=\"wp-block-heading\">Cron Execution Only<\/h6>\n\n\n\n<p class=\"wp-block-paragraph\">Pop this at the top of a bash script.<br><br>Eg. The below is for a cronjob that is executed at reboot:<br><br># crontab -l<br>@reboot \/bin\/bash \/path\/to\/something\/bad.sh<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/env bash<br><br>if [[ ! $(&lt; \/proc\/${PPID}\/comm) == cron* ]]; then<br>  echo \"Do NOT manually execute this script.  ONLY at reboot!!!!\"<br>  exit<br>fi<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a few things I find helpful :) Bash history with timestamp. This is more of an audit thing, but sometimes it's useful to know when you did something. If you type the command history into your shell. \u00a0You get back a list of the last X number of command executed. $ history | tail &hellip; <a href=\"https:\/\/cdblog.cdstealer.com\/?p=1528\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Bash Shell Tips<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[86,18,264],"class_list":["post-1528","post","type-post","status-publish","format-standard","hentry","category-gentoo","tag-bash","tag-linux","tag-shell"],"_links":{"self":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1528"}],"version-history":[{"count":5,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1528\/revisions"}],"predecessor-version":[{"id":2480,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1528\/revisions\/2480"}],"wp:attachment":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}