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


    Hi again!

    Here’s a kind of small enhancement for the ‘type’ command. Something like: “always wanted to know what functions I have but never dared to ask”. 😉
    This could be put to ~/.bashrc. Then try command:

    def
    

    And here’s the code:

    # def = type + show all functions and aliases
    # def [items]
    #
    def()
    {
        if [ "$1" != "" ] ; then
            type "[email protected]"
        else
            # Show all function names and aliases.
            # Show function names using the 'ls' command!
    
            local function_names=$(declare -F | sed 's|declare -[^ ]*||')
    
            if [ -n "$function_names" ] ; then
                echo "Function names:"
                local tmpdir=/tmp/tmp-$$.dir
                mkdir -p $tmpdir || return 1
                pushd $tmpdir >/dev/null
                touch $function_names
                ls $function_names
                popd >/dev/null
                rm -rf $tmpdir
            else
                echo "No functions."
            fi
            alias
        fi
    }
    

    Please report issues including all details! ;-)
    Show files here: cat "file" | curl -F [email protected] https://ptpb.pw/?u=1
    and show the URL.

  • And noticed that post #2 had a bug, here’s a new version:

    __common_sum()
    {
        local opts xx
        for xx in "[email protected]"
        do
            case "$xx" in
                -c) opts="--ignore-missing" ;;
            esac
        done
        command ${FUNCNAME[1]} $opts "[email protected]"
    }
    

    Please report issues including all details! ;-)
    Show files here: cat "file" | curl -F [email protected] https://ptpb.pw/?u=1
    and show the URL.

  • Hello again bash friends!

    Here’s a handy function for helping you to work a bit differently on the command line!

    If you wish to open a file, say mydoc.odt, and then another, like mytext.txt, you’d normally do that like this:

    libreoffice mydoc.odt
    gedit mytext.txt
    

    But now, with the trick below, you can open them easily by just writing the file names on the command line:

    mydoc.odt mytext.txt
    

    instead of remembering the names of programs that open them.

    The only thing it takes is: put the underlying bash function into your ~/.bashrc and enjoy!

    # This bash function enables opening files without specifying
    # the command that should open them. Just give the name(s) of file(s)
    # on the terminal command line.
    #
    # The implementation is based on two main factors:
    # 1. given file(s) are handled by the unknown command handler of bash
    # 2. mime types of files must be set up correctly
    #
    # Example: use just the names of the files:
    #    testfile.txt second-testfile.odt
    # instead of more tedious
    #    notepadqq testfile.txt ; libreoffice second-testfile.odt
    #
    # Put this function to your ~/.bashrc in order to use it automatically.
    #
    # Note: either command exo-open or xdg-open must be installed.
    #
    command_not_found_handle()
    {
        local good    # list of files found => they will be opened
        local bad     # list of unrecognized words => give error
        local opener  # command to open any file
        local tmp
    
        for tmp in /usr/bin/exo-open /usr/bin/xdg-open
        do
            if [ -x $tmp ] ; then
                opener=$tmp
                break
            fi
        done
    
        for tmp in "[email protected]"
        do
            if [ -f "$tmp" ] ; then
                good+=("$tmp")
            else
                bad+=("$tmp")
            fi
        done
    
        if [ "$good" != "" ] ; then
            $opener "${good[@]}" >/dev/null 2>&1 &
        fi
        if [ "$bad" != "" ] ; then
            echo "Error: '${bad[@]}' not found."
        fi
    }
    

    Please report issues including all details! ;-)
    Show files here: cat "file" | curl -F [email protected] https://ptpb.pw/?u=1
    and show the URL.

  • @manuel

    That’s a neat function. Thanks for sharing! 🙂

    Seared Vandal

tips6 bash6 mostly2 tricks1 Posts 24Views 1789
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.