Bashrc's alias issue

I am trying to create a permanent alias by either inserting it into the .bashrc directly (although it is just called bashrc in Fedora) located in /etc/ to the lowest part of bashrc using gedit. OR, by creating a .bash_aliases file, chmod to 755, and using the code below into bashrc.

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

But nothing is working, because of
Set document metadata failed: Setting attribute metadata::gedit-encoding not supported
Set document metadata failed: Setting attribute metadata::gedit-spell-language not supported

/etc/bashrc is the system-wide bashrc.

You don’t want to edit it. If you’ve mangled it and don’t know what to undo, please say what version of Fedora you’re on (36, 37, or 38) and I’ll point you to the original.

~/.bashrc is your user bashrc.

Feel free to edit it; add your aliases directly, or source ~/.bash_aliases here. If you’ve mangled it, you can copy the template from /etc/skel/.bashrc to ~/.bashrc

There should be no need to chmod files in your home dir, so I’m not sure where you created your .bash_aliases file. If you created /etc/.bash_aliases, you’ll want to remove it.

FYI, ~ refers to your home directory, i.e. /home/$USER (where $USER is your username).

4 Likes

Not true.
There is /etc/bashrc which is a system wide bashrc that applies to every user.
There is also /home/USER/.bashrc which provides user specific configurations and would normally be where the user adds their own specific customizations.

I prefer adding my profile customization to /etc/profile.d to make it work system-wide, avoid modifying configurations owned by packages, and save time merging configurations on system upgrade.

3 Likes

@computersavvy @jn64

Oh ****. I’d edit out the changes I’d made into it (even rm the .bash_aliases), although I think I had messed up. The warning Set document metadata failed::gedit- keep on appearing even when I exit gedit. And an interesting thing I just discovered, my VM’s snapshot (the one before I toy with the bashrc file), doesn’t seem to work properly. Nothing change when I restore it, as I can see the same browser pages and apps.

Should I reinstall gedit? Did that etc/bashrc edit cause this?

I found it, and now I’m trying to find .bashrc.d where I think it kept the aliases. Didn’t find it at home directory using ls -a.

User specific aliases and functions

if [ -d ~/.bashrc.d ]; then
for rc in ~/.bashrc.d/*; do
if [ -f “$rc” ]; then
. “$rc”
fi
done
fi

Any blog or article for me to understand what is profile.d, and know how to edit it? Didn’t read about anything about it regarding creating alias.

You would need to create that directory then put any files you want in there. Those files will not be modified during any system updates so you can consider them as static files.

Each file can contain one or more aliases.

You don’t have to use the ~/.bashrc.d/ folder, it’s just a different way to organise things. You can also do it the typical way with sourcing your own ~/.bash_aliases.

If you want to use ~/.bashrc.d/, just create the folder, and put any shell scripts in there that you want to be sourced. The included snippet in the default ~/.bashrc loops through every file in ~/.bashrc.d/ and sources it for you.

You can organise the files any way you like. For example you could have 1 file for aliases like ~/.bashrc.d/aliases.sh and 1 file for environment variables ~/.bashrc.d/env.sh. Or you could have separate files for different programs/contexts.

/etc/profile.d is like the system-wide equivalent of this concept. If you manage a multi-user system and want to set things for everyone, you could use it, but for a single user it doesn’t really have any advantages over doing things in your home dir, and is less convenient as you need root permissions to edit. Also, packages may install files in /etc/profile.d so it’s a lot more crowded (you probably already have dozens of files in there).

I don’t know what causes your gedit errors. Were you using it with sudo? Generally you should not use GUI apps with sudo.

1 Like

I have no idea where I got wrong again. It should be a simple matter of either adding the aliases directly into .bashrc. Or, creating a separate aliases file by including the below code too. But nothing happen.

# My custom aliases
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

##shortcut aliases
alias dnfuu=sudo dnf update -y && sudo dnf upgrade -y
alias ooo=cd ~/XXX
alias kkk=cd ../../..

As for sudo gedit, I did actually. Guess I forgot adding sudo is pointless.

This should be alias dnfuu="sudo dnf update -y && sudo dnf upgrade -y"
Although that command is doing exactly the same thing twice. With dnf the ‘update’ command is an alias for the ‘upgrade’ command.

Maybe he wants to execute the equivalent of Debian’s sudo apt-get update && sudo apt-get upgrade (or similar) which would be:

sudo dnf upgrade --refresh

1 Like

@vgaetera @computersavvy
Still doesn’t work, either with " or '. I saw some alias ignored the ", but I guessed that is because it only use 1 specific command like sudo reboot, or not.

So in dnf, update & upgrade does the same thing?

You need to re-source ~/.bashrc or open a new terminal to apply the changes.

This is correct:

alias foo="sudo reboot"

It means make foo an alias of sudo reboot.

This is wrong:

alias foo=sudo reboot

It means make foo an alias of sudo, and print the value of the alias reboot.

Yes. upgrade is the actual command, but update is an alias of it (this is a dnf internal alias, not a bash alias).

dnf automatically refreshes the cache on a timer, and also when it’s used if the cache is stale. So there is no need to update the cache manually like apt update.

For more information on common apt vs dnf commands, see:

1 Like

(Face palm) All I need was to open in a new terminal. (Sigh) Do I need to do this every time I create a new alias? Yes, I do.

Oh right. I forgot the neat feature of dnf and using fedora.

Thank everyone who had helped out.

Yep,
.bashrc only gets read when the terminal session is opened. We all have hit that roadblock in the past so don’t feel bad. Just remember it for the future. :upside_down_face:

You can also make an alias to execute :
source ~/.bashrc && source ~/.bash_profile (Of course you would also to source your alias file if external) this will reread your personal bash files.