1

I tried to implement the following line in my .bashrc,

alias ./my_exec='printf "foo"'

However, the alias doesn't work, and the following line appears :

bash: alias: `./my_exec': invalid alias name

I know that the zsh terminal can make this work, but I wouldn't switch to it for this only thing.

Is there is a way I can make this alias work ?

  • 3
    You need to use a valid name, such as: alias my_exec='printf "foo"'. Or, is there some reason that you want the ./ as part of the alias name? – John1024 Apr 12 at 23:37
  • Yes, the ./ is necessary to me – HeyShafty Apr 13 at 0:13
  • 1
    And, the reason for that is? If you want people here to provide useful help, it would help if you explained why you want to do this. – John1024 Apr 13 at 0:17
  • 1
    Prank a friend, so that his ./my_exec outputs "Segmentation fault (Core dumped)" I have zsh, so he could prank me by doing it, I just want payback – HeyShafty Apr 13 at 0:19
  • 2
    I'm sorry, be every reason is valid for everyone to learn new things. – HeyShafty Apr 13 at 17:10
1

maybe you need a function

my_exec(){
   print "foo"
}

this found for me,

countryip(){

  COUNTY="$(whois $1 | grep ountry))"
  echo "$COUNTRY $1 \n"

}

$ countryip 192.168.0.1
  • 1
    Wow it worked actually ``` ./my_exec() { printf "foo\n" } ``` – HeyShafty Apr 13 at 0:30
  • 1
    @HeyShafty also ../foo/bar(){ echo yeah; }; ../foo/bar I think you should make your own answer with that -- notice however that it only works in bash and zsh. – mosvy Apr 13 at 0:49
11
$ BASH_ALIASES[./my_exec]='echo yes'
$ ./my_exec
yes

According to the bash manpage, you cannot use / in an alias name:

The characters /, $, `, and = and any of the shell metacharacters or quoting characters listed above may not appear in an alias name.

When defining an alias via the alias name=val syntax, bash will refuse any alias name that contains any character defined by the regex:

[ \t\n&();<>|\\"'`$/]

See the legal_alias_name() function in its source code. Notice that the lack of = above is not an omission; the impossibility of using it in an alias name is simply an artifact of the syntax.

But you can use some of those characters in an alias, by defining it indirectly via the BASH_ALIASES array:

$ BASH_ALIASES['/a=$']='echo yes'; /a=$
yes

Alias names in the susv4 standard

3.10 Alias Name

In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set and any of the following characters: !, %, ,, @.

Implementations may allow other characters within alias names as an extension.

So both bash and zsh (which allows / to be used in alias names directly) are within the standard.

Slashes and other funny chars in function names

In bash and zsh, a / can be used directly in a function name:

$ /bin/sh(){ echo no bin/sh today; }
$ /bin/sh -c ls
no bin/sh today

This is a non-standard extension; in a standard shell, a function name can contain only ascii letters, digits and underscores, and cannot start with a digit.

In bash, a function name can be made up of any characters except $, with the condition that is doesn't contain only digits and within the constraints imposed by the function definition syntax. You can look at execute_intern_function() and check_identifier() for all the details.

In zsh the all-digits constraint doesn't apply, and a function name can also be quoted/escaped in the definition:

zsh$ 666() echo "$0"; \$() echo "$0"
zsh$ 666; $
666
$
-1

The question here is that we are forgetting that './' says to the shell 'looks at this file, command is here in this directory that I am now "current directory" and here in this case it is not necessary your presence because as we are seeing will occur a clear mistake! in case you should declare the alias normally just like any other alias

  • alias my_exec = 'echo "foo "'here you will get a better result. OK!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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