0

Basically I created an alias which is

alias 1="python /root/sqlmap-dev/sqlmap.py"

and when i type 1 it's working excellent but if i opened another terminal and typed 1 again. it's not recognize the alias !

so how to make the alias available everywhere ?

migrated from serverfault.com Apr 16 '17 at 18:31

This question came from our site for system and network administrators.

3

Aliases are specific to the sessions they were defined in. You need to define them in every session. Fortunately, there is a mechanic in place which does exactly that: It does the same stuff every time a new terminal session begins.

The file ~/.bashrc is executed every time you open a new terminal session as you (your user account). Note that its name starts with a period, so it's a hidden file. Make sure to display hidden files in your file browser (probably via Ctrl + H). ~/.bashrc is commonly used to define things like aliases or bash functions.

This does, however, not mean that you should define it there. On Debian and its derivatives like Ubuntu (and its derivatives like Kubuntu, Lubuntu, Mythbuntu, Ubuntu Budgie, Ubuntu GNOME, Ubuntu Kylin, Ubuntu MATE, Ubuntu Studio, Xubuntu, etc.), Deepin, Whonix, etc., user-defined bash aliases usually are stored in ~/.bash_aliasas which on most of those systems doesn't exist by default, so if you're on one of those, go ahead and create it. It automatically is invoked if it exists.

If your system doesn't invoke that file automatically, you can still receive the luxury of being able to define your bash aliases (There can be a lot of those! Like a lot a lot! I have about 60 on my main account of my main machine and wouldn't want them to just be somewhere in my ~/.bashrc.) in a dedicated place. Just add these lines to your ~/.bashrc:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Note that this file is invoked when you start a terminal session. Changes to that file aren't retroactively applied to open terminal sessions, meaning that you can't use newly defined bash aliases in terminals you opened prior to defining/modifying the aliases in that file.

2

The problem is with the command alias=... you are creating an alias for the current shell session, so you'll have to define the alias for each new one you start, i.e. each time you open a terminal.

To make the alias persistent, put the definition in your shell's configuration file, for example ~/.bashrc for bash.

-2

Put it in your shell's configuration file, e.g. ~/.profile.

  • 2
    .profile would only work in a login shell. Opening a terminal doesn't create a login shell. – Gilles Apr 16 '17 at 21:28
  • @Gilles: Well, as OP neglected to name the shell, I named the most portable file for this case. It will be read and used at the next login at the latest (and can easily be sourced until this is done). Also, from a "philosophical" point of view, one should keep ~/.bashrc etc. as short as possible and put everything that only needs to be set once a session (at login) into a file read only by login shells. An alias fits this bill for me. Yes, setting an alias has no measurable impact on a modern system, but I still keep this rule. Also, you might have noticed OP stated that it worked for him... – Sven Apr 16 '17 at 21:43
  • 2
    @Sven Aliases are not inherited by subprocesses, so defining an alias at session startup time does not work outside the login shell. – Gilles Apr 16 '17 at 22:02

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.