[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Can a Bash function be named "w3m" ?



On Thu, Jan 30, 2025 at 13:59:37 +0100, Roger Price wrote:
>  alias w3m='/usr/bin/w3m -no-cookie -o auto-image=TRUE '

>  w3m() { /usr/bin/w3m -no-cookie -o auto-image=TRUE $@ ; }
> 
> and received the error message
> 
>  bash: .bashrc: line 86: syntax error near unexpected token `('
>  bash: .bashrc: line 86: `w3m() { /usr/bin/w3m -no-cookie -o auto-image=TRUE $@ ; }'

The problem is you *already* have the alias, and the alias gets expanded
when you try to define the function.

hobbit:~$ alias grok=true
hobbit:~$ grok() { echo hi; }
hobbit:~$ grok
hi
hobbit:~$ true
hi

Aliases are insidious like that.

> I wrote
> 
>  [[ $(type -t w3m) == "w3m" ]] && unalias w3m

Well, that doesn't work.  I'm not sure what you were going for there,
but that's not what "type -t" says for an alias.

hobbit:~$ type ll
ll is aliased to `ls -l'
hobbit:~$ type -t ll
alias

To answer the question in the Subject header: yes, a function may be
named w3m (without the quotes).  Bash allows a stupidly large range
of characters to be used in function names, including forward slashes.

hobbit:~$ /bin/rm() { echo haha; }
hobbit:~$ ls -l badfile
ls: cannot access 'badfile': No such file or directory
hobbit:~$ touch badfile
hobbit:~$ /bin/rm badfile
haha

It's just your alias that was in the way.

Even if you were writing for a pure POSIX sh (not that any such thing
exists in reality), w3m would be a legal function name, because it's
a legal variable name.  All legal variable names are also legal function
names under POSIX rules.


Reply to: