• Bash tips and tricks (mostly for the command line)

    Hi all!
    Would you like to have a place for sharing your bash tips and tricks, i.e. command line magic and scripts that you find useful and think may be helpful to others as well? You’ve come to the right place!

    Probably many of the tricks to be presented here can be saved into your $HOME/.bashrc file to be easily available also later. Other tips may be even readily useful, executable bash programs that can be installed e.g. to the common application directories (in $PATH variable) such as /usr/local/bin or /usr/bin.

    If you wish to share a bash tip/trick, please

    • write it clearly
    • add some comments how to use it
    • preferably add comments also about the implementation so that others can learn!

    Note that many people relate bash to command line only, but there are packages available to add GUI features, too, such as yad (that was my first tip here ;)).

    So, to see another bash tip, see the next post here!

    Happy bashing! ;)

  • Here are some definitions that help verifying checksums of downloaded files, e.g. ISO files.

    __common_sum() { command ${FUNCNAME[1]} --ignore-missing "[email protected]" ; }
    md5sum()    { __common_sum "[email protected]" ; }
    sha1sum()   { __common_sum "[email protected]" ; }
    sha224sum() { __common_sum "[email protected]" ; }
    sha256sum() { __common_sum "[email protected]" ; }
    sha384sum() { __common_sum "[email protected]" ; }
    sha512sum() { __common_sum "[email protected]" ; }

    These definitions “override” some programs that can verify checksums. The override adds an option ‘–ignore-missing’ to all of the checking programs, thus avoiding some unnecessary warnings when checking the downloaded file.

    For example, program md5sum can check a downloaded ISO file (e.g. antergos.iso) and the accompanying md5 checksum file (e.g. antergos.iso.md5) like:

    md5sum -c antergos.iso.md5

    and tells if the iso file OK or corrupted.
    Now if the antergos.iso.md5 file contains checksums for many .iso files and you have downloaded only one of them (antergos.iso), the md5sum program gives you warnings about other missing iso files.
    With the trick above, those warnings are not displayed.

    Note that the definitions do not replace the real programs, but only modify how you call them during a command line session.

    And you can use similar techniques to modify any usage of practically any program.

    Of course you could use bash aliases instead of bash functions, but aliases are meant for much more simple tasks than functions.

    For more information,

    man bash
  • I posted this somewhere else once. Here it is now for the Antergos forums:

    I’m starting with ~/.inputrc.
    This file is used by the Readline library, which is the library used by all Bash shells to control the command line.

    1. history search with up and down arrow keys

    "\e[A": history-search-backward
    "\e[B": history-search-forward
    "\e[C": forward-char
    "\e[D": backward-char

    This one somewhat common, but I am still surprised to see so many users without it. The up and down keys work up and down the shell history even during command completion. The last two lines aren’t needed on many shells. But they fix a problem on other shells where you were not able to move left and back between characters with the cursor keys.

    2. ctrl-right and ctrl-left keys move between words

    "\e[1;5D": backward-word
    "\e[1;5C": forward-word

    Useful if you are not an emacs or vi person. Also useful if you are.

    3. single tab for completion; complete the word as far as possible and show remaining possible completions

    set show-all-if-ambiguous on

    This one fixes a common annoyance in bash, in which we are forced to double tap the TAB key to trigger completion. Well, not anymore! It does to things: From now on you just need to hit TAB once (yey!) and it also completes a command as far as possible before showing further completion options.

    4. invoke man page for the command in the current line with alt+h

    "\eh": "\C-a\eb\ed\C-y\e#man \C-y\C-m\C-p\C-p\C-a\C-d\C-e"

    It doesn’t matter where you are in the command line. Having trouble with a command you are typing? Can’t remember some of its options? Hit ALT+h to invoke man for that command and when you return it will still be there waiting for your input.

    5. automatically add sudo to the beginning of the line with alt+s?

    "\es":"\C-asudo \C-e"

    Similar to the above, it doesn’t matter where you are in the command line. Forgot to write sudo at the beginning of the command line? Then just hit ALT+s and continue typing your command.

    If you look carefully at the two commands above, you can use them to make your own new key triggers. Have fun.

    ~/.bashrc on the next post…

  • Concerning ~/.bashrc

    1. Auto cd into a path

    shopt -s autocd

    Just type the directory path and bash will cd into it. Works also with ~.

    2. Slim down your history file

    export HISTIGNORE="cd*:ls*"

    This is a colon-separated list of command patterns that you do not wish to go into the shell history file.

    Here’s my own pattern string: cd*:l[s|l]*:clear:exit:.*: *.
    This will disable history for the cd, ls, ll (an alias), clear and exit commands. In addition it disables history for commands that start with a . (dot) (for a couple of alias I have). Finally it also disables history for commands that start with a space. If I don’t want a certain command to be sent to history, I can just start it with a space.

    3. Disable terminal suspend feature

    stty -ixon

    You can issue this command on-demand, or just save it in your .bashrc file. I prefer the latter. What it does is allow you to use the ctrl+s key combination in your programs. Otherwise every time you hit ctrl-s, your terminal will lock and you need to hit ctrl-q to unlock it.

    • Vim is one program that likes this setting. ctrl-w_ctrl-s is used to split windows.
    • Another one is while doing reverse incremental search in your shell history file with ctrl+r. If you overshoot your target, you can reverse the search with ctrl-s and this will no longer lock your terminal.
    • And you are now also free to map this keyboard combination to any other terminal program of your choosing.

    5. Display your path as a vertical list

    path() ( IFS=: ; printf '%s\n' $PATH ; )

    I don’t intend to show aliases, but this one is useful and generic enough it deserves being mentioned. It displays your path as a much more readable vertical list. From now on, just invoke $ path on your terminal to see your $PATH in high definition.

  • Really nice tips, @Krugar !

    About your .inputrc tips: some of those settings can be put to .bashrc (with different syntax) instead of .inputrc, and at least for me it is easier since all command line settings would be in one place.
    For example, the up and down arrow keys can be put to .bashrc as follows:

    bind '"\e[A":history-search-backward'    2>/dev/null
    bind '"\e[B":history-search-forward'     2>/dev/null

    (Note that the redirection above is not usually necessary.)
    So, is it possible to put all of those .inputrc tips to .bashrc (with different syntax probably)?

    Overall, it is very nice to learn about tips like these, since we all like to use our environments the way we like, and it actually is quite easy to modify the environment to your liking. :)

  • @Krugar

    One feature that has has been a bit annoying for me can be changed in ~/.inputrc like this:

    set mark-symlinked-directories on

    When doing completion with a symlink that points to a directory, this setting allows a trailing slash to be added.

  • @Krugar About your man page invocation (which is really nice!): I modified it slightly for ~/.bashrc:

    export MY_TRM=xfce4-terminal      # give your favourite terminal here!
    bind '"\eh": "\C-a\eb\ed\C-y\e#$MY_TRM --execute man \C-y\C-m\C-p\C-p\C-a\C-d\C-e"'

    Now Alt-H opens another window for the man page and leaves the original command window as is.

  • @manuel said in Bash tips and tricks (mostly for the command line):


    Hmm… Unfortunately there isn’t a standard way for doing this. The above is only going to work on gnome terminal, its forks and terminals inspired by it, like MATE terminal or the Xfce4 terminal, for instance. Other common terminals like urxvt, termite, terminology, etc, have the -e, or --exec options. Could be scripted though, since you moved it to .bashrc… so it’s not a biggie.

    It’s not a bad thing to have on stacking window managers where opening a separate terminal and keeping both visible is a much slower operation than on tilling WMs. But I would perhaps suggest also keeping the old one and changing the binding on this one to a capital H so that it gets activated instead by shift+alt+h.

  • @Krugar
    You’re right about the --execute option. It is better to include the option to the variable, e.g.

    export MY_TRM="xfce4-terminal --execute"

    and remove ‘-e’ from the ‘bind’ line.
    Option --execute seems to work for xfce4-terminal, qterminal and terminator, these are the terminals I usually use.
    Your shift-alt-h is also a great idea.

  • A useful tip when running bash programs. Put the following line into your ~/.bashrc:

    export FUNCNEST=50    # limit depth of recursion in bash function calls

    This is meant to prevent deep recursion (due to a programming error) in a bash script from severely slowing down or crashing your machine.

  • No one has mentioned Aliases. These are powerful shortcuts you can put in you .bashrc
    You can also replace the actual command with an alias as bash searches aliases first.

    Here are the ones I use the most.

    alias lls='ls -lh'
    alias sudo='sudo '
    alias update='sudo pacman -Syu'
    alias yaourt='yaourt --noconfirm'

    ls -lh (list humanly readable), try it and you wont go back ;)

    The sudo alias is to make other aliases work with sudo.
    I use a lot more, but these are specific to my workload and would most lightly not fit you, but out your aliases in a git and its easy to import on a new system!

  • @izznogooood

    Thanks for your input! Nice aliases.
    And this is another good example of how easy it is to modify your environment to your very personal liking. :)

  • alias yaourt='yaourt --noconfirm'

    Please, please, don’t do or advocate this if you value your system stability or that of others.

    It’s bad enough the widespread use of yaourt on Antergos and other Arch derivative distros, despite being strongly advised against for quite some time. But it is much, much worse, to allow it to run without verifying PKGBUILD file contents. The AUR is software source managed by users and free for all with very few peer review barriers. Anyone can provide build files which run with root privileges. Some of which may be ill intentioned and others may simply contain bugs that break the system.

    Much like the use of PPAs on a debian system, the AUR demands care while using and not bash aliases that obfuscate or neutralize its already meager security features.

    alias update='sudo pacman -Syu'

    Avoid the use of sudo in your aliases. Always prefer them without sudo and force yourself to explicitly use sudo on the command line. It’s both an healthy security habit, but it also enables you to use sudo features on a case-by-case basis. For instance, sometimes I wish for a command to inherit my current environment and I will type sudo -E command. I won’t be able to do that if I have already aliased the command to alias=sudo command

    So, on your example above, the alias should simply read alias update=pacman -Syu. And that also avoids that ugly sudo alias you have there, that can become problematic when trying to invoke sudo inside scripts.

  • @Krugar

    That is true. Just as you said, anyone can make a package to AUR, and nobody (except you) is checking if there are any malicious stuff. You are on your own with AUR, and really should check those PKGBUILD files if you intend to use any AUR package. If you don’t know what the PKGBUILD is doing, it is best not to install that package.

  • I wonder why they say linux is not user friendly 😞 🔫

  • @izznogooood
    Hopefully you see that we mean well here.
    People new to Linux and/or new to Antergos/Arch may read this thread as well, and it is just wise to give solid advice to them right from the beginning. That’s why questionable practices should be avoided, at least publicly.
    Linux makes many things quite easy to do.

    But we all learn good things here! :)

  • Well, it is. It just takes a bit more work and time to be so. Consider the following:

    Every abstraction that translates a power feature into a user friendlier feature comes with a cost. That cost is usually the removal of some power in favor of some friendliness. It’s always a balance and as you raise one side of the seesaw, you’ll invariable lower the other side.

    To combat this, an alias is a weak tool, because as you have just seen, it is virtually featureless itself. Instead you would need a bigger tool. Bash scripting or Python, for instance, have the power to turn power into friendliness without sacrificing anything (depending on what one wants to do, though).

    So, you can have plenty of user friendliness on Linux. And a proof of that is desktop environments and their tools. But you also have to come to accept that Linux is a powerful operating system that exposes all of its innards to the users. And this means that providing a user friendly feature while retaining all aspects of security and computer usage best practices, takes work and is not something you can easily achieve with a simple tool as the bash alias.

  • “this is the chaos of open source”

    This is one of my favorite quotes. It emphasizes the difficulty of getting things done in an environment where everyone is opinionated an has the possibility to “fork” (think of a similar word…) a project.

    Many also assume people are idiots. The simplest command can become deadly if used the wrong way. Give a man a match and he can make a fire, or burn down the world.

    My point being, if you don’t know what you’re doing you should not be doing it, unless you can afford to loose. Give warnings yes, recommendations of course. But please, don’t scare people away with religious security or bias “best practice” use-cases. That’s my final post on the matter.

    Happy bashing.

    @krugar Please dont take this as a personal attack, its simply a very different opinion. People will learn in due time.

  • @izznogooood said in Bash tips and tricks (mostly for the command line):

    Give warnings yes, recommendations of course. But please, don’t scare people away with religious security or bias “best practice” use-cases. That’s my final post on the matter.

    I have an enough bad reputation on the Antergos forums as a troublemaker and a grouch, to now start an argument with you, so I am glad this is, as you say, your last word on the matter.

    I’m sad you took my advise this way. I shall refrain from giving any more of that to you, since you clearly haven’t learned yet how to take advice and it bothers you. I have a good memory, so don’t worry it won’t happen by accident either. Have a better day than the one I apparently gave you.

  • @Krugar @izznogooood


    please stop here, as it is gone off the topic.
    But feel free to open a thread here: where ou can discuss such stuff:

bash7 tips6 mostly2 tricks1 Posts 30Views 3562
Log in to reply
Bloom Email Optin Plugin

Looks like your connection to Antergos Community Forum was lost, please wait while we try to reconnect.