Differences between revisions 5 and 6
Revision 5 as of 2016-09-25 19:52:37
Size: 757
Editor: 46
Comment: change echo to printf, remove useless { }
Revision 6 as of 2020-04-30 07:20:44
Size: 774
Editor: c-73-202-78-216
Comment: {}-delimit the function body. It's not needed in this particular example, but it's much simpler (especially for newbies trying to move away from aliases) to just always brace-delimit their functions.
Deletions are marked like this. Additions are marked like this.
Line 10: Line 10:
settitle() case $TERM in *xterm*|*rxvt*) printf '\e]2;%s\a' "$1"; esac settitle() {
  
case "$TERM" in *xterm*|*rxvt*)
   
printf '\e]2;%s\a' "$1"
 
esac
}

How can I make an alias that takes an argument?

You can't. Aliases in bash are extremely rudimentary, and not really suitable to any serious purpose. The bash man page even says so explicitly:

  • There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see FUNCTIONS below).

Use a function instead. For example,

settitle() {
  case "$TERM" in *xterm*|*rxvt*)
    printf '\e]2;%s\a' "$1"
  esac
}

Oh, by the way: aliases are not allowed in scripts. They're only allowed in interactive shells, and that's simply because users would cry too loudly if they were removed altogether. If you're writing a script, always use a function instead.

BashFAQ/080 (last edited 2020-04-30 07:20:44 by c-73-202-78-216)