Let me explain with an example:
I have a line where I declare an alias in my ~/.bashrc
:
> grep lsdir ~/.bashrc
alias lsdir='ls -d */'
I have just added this line to my bashrc, and the alias is thus not yet added to my current session environment. I would also like not to re-source all my bashrc for some configuration reason, which is why I would like to simply run this grepped line on its own.
For the purpose of curiosity and education, I tried to do it without having to write it down manually, to no avail.
I tried this:
> $(grep lsdir ~/.bashrc)
bash: alias: -d: not found
bash: alias: */': not found
or this:
> "$(grep lsdir ~/.bashrc)"
bash: alias lsdir='ls -d */': No such file or directory
or even this:
> $("grep lsdir ~/.bashrc")
bash: grep lsdir ~/.bashrc: No such file or directory
But none worked. Is there any way to achieve this ?
$HOME/.bash_aliases
, and put your aliases in there, and source it inside your.bashrc
; then when you need to update aliases, source only that file. You could even doalias updatealiases='source $HOME/.bash_aliases'
. – frabjous Mar 28 '22 at 18:39.bashrc
blindly, and those parts do not necessarily work the same as they do at start up. What if in the original.bashrc
there's code inside a conditional which is no longer in a conditional when pulled out with grep, or uses a variable that is defined differently in the interactive shell? – frabjous Mar 28 '22 at 19:56alias lsdir='ls -d */'
, gets word-split toalias
,lsdir='ls
,-d
,*/'
which isn't what you want, but more like running the command linealias "lsdir='ls" -d "*/'"
. (And the*/'
gets expanded to any matching filenames if there are any, and if there aren'tfailglob
ornullglob
will trigger.) – ilkkachu Mar 29 '22 at 13:15