Solved How to save what was written to alias

Every once in a while I type alias to freebsd shell(sh and bash), however after rebooting I got the same problem: aliases are not saved. How do I save it, so that it would be there for the rest of this PC's life. Also how do I copy them to bash and to other users.
I do alias with following command:
alias grep="grep --color=auto"
Also I have got another problem/
When I type quickly I type "pkg isntall" instead of "pkg install"
How do I set alias to this?

Thanks in advance
 
I am using shells/fish exclusively (yes, even for root) since years, so all I care about is my site-wide configuration /usr/local/etc/fish/config.fish. So unfortunately I am not very proficient in the handling of the startup files of other shells, especially that abomination shells/bash.
You can try putting your aliases in ~/.bashrc. If that does not work, than read the manpage for bash() and look for the section where it describes the startup files.
 
I don't know if I'd set aliases for mistypes, just because it seems to encourage bad habits. (I also, from time to time, see lists making fun of people's aliases, such as alias kitty=cat and so on, and I'd be afraid to get on one of those because I'm so insecure I care what people who will never meet me think of me.) :)
Anyway, to set an alias for bash, you can put it in your $HOME/.bashrc or $HOME/.bash_profile For sh, you can put them in $HOME/.shrc, you'll see there are some aliases already there, though I assume it could also go in $HOME/.profile.
 
Every once in a while I type alias to freebsd shell(sh and bash), however after rebooting I got the same problem: aliases are not saved. How do I save it, so that it would be there for the rest of this PC's life. Also how do I copy them to bash and to other users.
I do alias with following command:
alias grep="grep --color=auto"

Eh, yes.
  1. for /bin/sh, in the users homedir I have .shrc. Here you can put aliases for this user. Then also in .shrc I have this:
    [ -r /etc/shell-aliases ] && . /etc/shell-aliases
    This /etc/shell-aliases can be created and can hold these aliases anybody should get.
  2. for /usr/local/bin/bash there is in the homedir .bashrc, that can get the same
    [ -r /etc/shell-aliases ] && . /etc/shell-aliases
    But, at login, bash does not run .bashrc. Instead it runs .bash_profile. So make that happen nevertheless and put into .bash_profile:
    [ -r $HOME/.bashrc ] && . $HOME/.bashrc
  3. for /bin/csh the alias has a slightly different syntax:
    alias ll ls -lAF
    csh is only used by root here, so I put these just into /root/.cshrc
Caveat: this here has grown over many years, and features of the shells may have slightly changed over time. So it may not be fully accurate, or there may now other/simpler ways to achive it. When in doubt, check with the manpages.

If you frequently create new users, there is a means to install prototypes for these .dot files, which the new user will then automatically get. Check the doc for the user-creation command - I rarely use that.

Also I have got another problem/
When I type quickly I type "pkg isntall" instead of "pkg install"
How do I set alias to this?

I would NOT do that. The unix commandline is a precision tool, and a single missing or added letter may have serious effects. So better train yourself to do correct typing, and/or get a keyboard that facilitates such (I am using original IBM RS/6000 keyboard).
 
Agreed. I used to have games/sl installed and added aliases as necessary in an attempt to cure my mistyping of certain words. Unfortunately, this doesn't work so well these days since many programs like pkg(8) and git(1) use subcommands. It also doesn't scale when dealing with programs that run other programs like doas(1) or sudo(8).
Thanks for the information. In that case I want to set flags as defult, so that if I type, for example, grep command it will output grep --color=auto, or in case of subcommands if I type pkg install it would understand pkg install -y.
I assume that this is entierly different process with differert configuration.
Thanks in advance
 
Thanks for the information PMc . I have got another question. What happens when I type alias grep="grep --color=auto". Where does it stored and what is outputed when I type just alias command on shell. Can I "alias" it to alias to .shrc
 
Thanks for the information PMc . I have got another question. What happens when I type alias grep="grep --color=auto". Where does it stored and what is outputed when I type just alias command on shell.

If You type the alias command in the commandline, the alias is stored in the memory of the currently running shell process. It disappears when the shell terminates. You can see that when starting a subshell:
Code:
$ /bin/sh
$ type ll
ll is an alias for ls -laFo
$ /bin/sh
$ alias ll="fnord"
$ type ll
ll is an alias for fnord
$ exit
$ type ll
ll is an alias for ls -laFo
$

If you write it into $HOME/.shrc, then every shell will load that before doing anything else.
 
Back
Top