0

I have a collection of bash commands (one liners) that I use on a regular basis to open new shells or start programs with a given set of arguments. As I use dmenu, I'd like to be able to use those commands from my shell and from dmenu. So defining an alias in my .bashrc isn't enough, I have to put these commands in a dedicated file. The file needs then to be executable and stored in a $PATH known to dmenu. If I do so, the alias becomes useless because the shell will find the executable. But that means I have to create one file per command. It's annoying.

Is there a way to store all those commands in one file (or at least in a more convenient way) so that they can be accessed via shell and dmenu? Maybe something like an alias file read by dmenu...

    2 Answers 2

    2

    If you have all your aliases in a single file (let's say ~/.aliases.sh), you could do something like this:

    #!/bin/bash . ~/.aliases.sh alias | awk -F '[ =]' '{print $2}' | dmenu | xargs -ICMD bash -O expand_aliases -c $'. aliases.sh\nCMD' 
    2
    • I like your answer but this doesn't merge the executable with the alias. It only works for alias..For me, It's a step in the right direction, but not the full story. I'll work on this when I'll have time.
      – PinkFloyd
      CommentedApr 5, 2024 at 20:45
    • If I were in your situation I would simply adopt the "collection of small script files" option, since that makes everything available regardless of what shell you're using (or even if you're not using a shell and something is just calling exec()).
      – larsks
      CommentedApr 5, 2024 at 21:25
    0

    You will have to write a script to pipe them into dmenu and execute the result.

    this is a example script

    #!/bin/bash cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"} if [ -d "$cachedir" ]; then cache=$cachedir/dmenu_run else cache=$HOME/.dmenu_cache # if no xdg dir, fall back to dotfile in ~ fi if [ -f ~/.bash_aliases ]; then aliases=( ~/.bash_aliases ) fi if [ ~/.bash_functions ]; then functions=( ~/.bash_functions ) fi source $aliases cmd=`( IFS=: if stest -dqr -n "$cache" $PATH || stest -fqr -n "$cache" "$aliases" || stest -fqr -n "$cache" "$functions"; then ( stest -flx $PATH alias | awk -F '[ =]' '{print $2}' compgen -A function ) | sort -u | tee "$cache" | dmenu "$@" else dmenu "$@" < "$cache" fi )` if [ -f ~/.bash_aliases ]; then if [ ! -z "$(grep '^alias' $aliases|cut -d'=' -f1|grep $cmd)" ] || [ -z $(which $cmd) ]; then echo -e "source ~/.bash_aliases \n $cmd" | bash -O expand_aliases & else exec $cmd & fi fi 

    source

      You must log in to answer this question.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.