Make a Bash alias that takes a parameter?

后端 未结 20 1942
长发绾君心
长发绾君心 2020-11-21 06:53

I used to use CShell (csh), which lets you make an alias that takes a parameter. The notation was something like

alias junk=\"mv \\\\!* ~/.Trash\"

20条回答
  •  独厮守ぢ
    2020-11-21 07:20

    Here's are three examples of functions I have in my ~/.bashrc, that are essentially aliases that accept a parameter:

    #Utility required by all below functions.
    #https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-bash-variable#comment21953456_3232433
    alias trim="sed -e 's/^[[:space:]]*//g' -e 's/[[:space:]]*\$//g'"
    

    .

    :<

    .

    :<

    .

    :< /tmp/history
    
            #Clear all items in the current sessions history (in memory). This
            #empties out $HISTFILE.
            history -c
    
            #Overwrite the actual history file with the temp one.
            mv /tmp/history "$HISTFILE"
    
            #Now reload it.
            history -r "$HISTFILE"     #Alternative: exec bash
        else
            echo "Cancelled."
        fi
    }
    

    References:

    • Trimming whitespace from strings: How to trim whitespace from a Bash variable?
    • Actual line breaks: https://stackoverflow.com/a/4296147/2736496
    • Alias w/param: https://stackoverflow.com/a/7131683/2736496 (another answer in this question)
    • HISTIGNORE: https://superuser.com/questions/232885/can-you-share-wisdom-on-using-histignore-in-bash
    • Y/N prompt: https://stackoverflow.com/a/3232082/2736496
    • Delete all matching items from history: https://unix.stackexchange.com/questions/57924/how-to-delete-commands-in-history-matching-a-given-string
    • Is string null/empty: http://tldp.org/LDP/abs/html/comparison-ops.html

提交回复
热议问题