2

I want to add a flag to alias, in my case I want do it for bat. e.g.

mybat -a = 'bat --paging=always [file e.g .zshrc]'
mybat -n = 'bat --paging=never [file e.g .zshrc] '

I also wrote and tested the following code, but it did not work:

mybat() {
  local OPTIND value
  while getopts ":an" option; do
    case $option in
      a) value=never ;;
      n) value=always ;;
      ?) echo "invalid option: $OPTARG"; return 1 ;;
    esac
  done
  bat --paging=$value
}

Anyone have an idea?

3
  • I really get rid of nearly all alias and switch to functions, stackoverflow.com/questions/7131670/… .
    – PeterMmm
    Commented Sep 16, 2021 at 18:44
  • 1
    How did you run your function? Commented Sep 16, 2021 at 18:53
  • Ummm. How did you run my function? I run like this now: set -xv; mybat -a .zshrc; set +xv Output: set -xv; mybat -a .zshrc; set +xv +/bin/zsh:8> mybat -a .zshrc +mybat:1> local OPTIND value +mybat:2> getopts :an option +mybat:3> case a (a) +mybat:4> value=never +mybat:2> getopts :an option +mybat:9> bat '--paging=never Normally, I run like this: mybat -a .zshrc But it does not work, it does not show me the contents of the file (.zshrc file in my case).
    – Zeroday
    Commented Sep 16, 2021 at 20:54

2 Answers 2

1

I don't have bat so I substituted cat and added a few cat-specific flags:

$ man cat
   -A, --show-all
          equivalent to -vET

   -b, --number-nonblank
          number nonempty output lines, overrides -n

   -E, --show-ends
          display $ at end of each line

   -n, --number
          number all output lines

One function approach where we simply replace the first short option with its corresponding long option:

mycat() {

unset value

inputs="$@"
option="${inputs%% *}"                    # assumes only looking to parse first input arg

case $option in
    -A) value='--show-all';        shift ;;
    -b) value='--number-nonblank'; shift ;;
    -E) value='--show-ends';       shift ;;
    -n) value='--number';          shift ;;
esac

set -xv
cat ${value} "$@"                         # don't wrap ${value} in double quotes otherwise mycat() call with no flags will be invoked as "cat '' <filename>" and generate an error
set +xv
}

NOTE: remove the set -xv/+xv once satisfied the function is working as planned

Test file:

$ cat words.dat
I am a man      post-tab           # <tab> between "man" and "post-tab"
I am not a man I
am a man

Taking the function for a test drive:

$ mycat -n words.dat
+ cat --number words.dat
 1  I am a man      post-tab
 2  I am not a man I
 3  am a man

$ mycat -A words.dat
+ cat --show-all words.dat
I am a man^Ipost-tab$                    # ^I == <tab>
I am not a man I$
am a man$

$ mycat -b words.dat
+ cat --number-nonblank words.dat
     1  I am a man      post-tab
     2  I am not a man I
     3  am a man

$ mycat -E words.dat
+ cat --show-ends words.dat
I am a man      post-tab$
I am not a man I$
am a man$

$ mycat -A -n words.dat                  # 2x options; only replace 1st option
+ cat --show-all -n words.dat
     1  I am a man^Ipost-tab$
     2  I am not a man I$
     3  am a man$

$ mycat words.dat                        # no options
+ cat words.dat
I am a man      post-tab
I am not a man I
am a man
0

Sticking with the alias approach, define 2x different aliases, eg:

mybata='bat --paging=always'
mybatn='bat --paging=never'

NOTE: pick aliases that are short-to-type and easy-to-remmber, eg, mybata vs mybat_a vs mybat-a vs mba ...

1
  • Thanks and yes this way working fine, but I want use flag.
    – Zeroday
    Commented Sep 16, 2021 at 20:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.