3

I'm wondering why it is required to put ~/ before .bashrc when opening the bashrc file.

To illustrate:

I normally open files on my system as follows:

vim filename.extension 

But while in the /home directory if I do the following:

vim .bashrc 

vim will open a new file called .bashrc

In order to open my bashrc file I must do as follows:

vim ~/.bashrc

Why?

My current system is Linux Mint 18.3

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
MarkMark
  • 583
  • You can save the file (:w) and see where it was saved. That should be the directory where you opened vim. – Al.G. Jun 13 '18 at 09:48
  • ~ is expanded to your home directory, so it doesn't make sense. Are you sure your $PWD is the same as your $HOME? – choroba Jun 13 '18 at 09:49
  • @choroba I made a mistake and was not aware that ~ gets expanded to home directory (i.e. /home/username). Thanks for your help – MarkMark Jun 13 '18 at 13:07
  • @AI.G Thanks. When saving a file in Vim using (:w) the filename is shown at the bottom left-hand side of the screen but the file path to which it was saved is not visible. – MarkMark Jun 13 '18 at 13:10
  • 1
    note: it is filename not filename.extension Unix does not have file extensions. a . us just a .. Except when the . is the first character, in this case it tells ls not to list it (it is hidden). – ctrl-alt-delor Jun 13 '18 at 14:18
  • 6
    /home is not the home you are looking for. $HOME is where the home is. Your home will (probably) be /home/yourname. – ctrl-alt-delor Jun 13 '18 at 14:20

2 Answers2

22

Your difficulty might come from this:

while in the /home directory

.bashrc isn’t in /home, it’s in your home directory (often /home/username, and yes, it’s confusing), which you can go to by typing

cd

Once you’re there,

vim .bashrc

will open the existing file.

Always using

vim ~/.bashrc

means you never need to think about where you are ;-).

ilkkachu
  • 138,973
Stephen Kitt
  • 434,908
  • 2
    More intuitive when the users' directories were in /usr ;-) – Kusalananda Jun 13 '18 at 09:53
  • @Kusalananda when was that using Nux for 10 years never seen this – Kiwy Jun 13 '18 at 10:20
  • 2
  • 3
    @Kiwy Some FreeBSD systems use /usr/home/username for the home of username. – Kusalananda Jun 13 '18 at 10:38
  • @Stephen Kitt and ilkkachu Thanks, very stupid mistake on my part. I was unaware that '~' gets expanded to the home directory and was also unaware that in general when Linux users refer to the "home" directory they actually mean '/home/username' not '/home'. In any case this your answer is really beneficial as I don't have to keep cding back to '/home/username' when I want to execute scripts which reside there, I can simply do the following: command ~/file from anywhere in the system and have the desired outcome while remaining in my current directory. Thank you – MarkMark Jun 13 '18 at 13:15
  • 1
    Staying closer to the present day, a modern MacOS user's home directory is not /home/$USER but /Users/$USER – Law29 Jun 13 '18 at 18:18
  • You can also use the alias command to make common commands easier. For example as a DBA, when I switch to point to a different database I use the setenv alias rather than typing "alias setenv='. /home/oracle/scripts/bin/set_oracle_env.sh '". When I want to see what database I am pointing to I use the getenv alias rather than typeing "alias getenv='env | egrep '''ORACLE|TNS''' | sort'". If there is a command that you type often, just add it as an alias to your .bashrc script. By the way if you added your $HOME directory to your $PATH, you would not need the tilda, ~. – Gandolf989 Jun 13 '18 at 18:40
  • @Gandolf989 I would highly advise staying away from aliases (especially as a beginner). Yes, aliases can be helpful. But if you rely too heavily on aliased commands, then you are only hindering yourself. Imagine moving over to someone else's machine to troubleshoot a problem they are having and typing myawesomealias just to see myawesomealias: command not found. In that case, if you have relied on the alias to do the heavy lifting for you (instead of learning the syntax of the command yourself), then you have only ran yourself into another hole. – Jebby Jun 13 '18 at 22:08
  • @Gandolf989 With that being said. I believe that some trivial aliases are fine. But as a beginner, you should ___not___ be relying on them – Jebby Jun 13 '18 at 22:13
  • Hi Jebby, If you want to know just type alias and press enter and you will see what is set. When I started learning Unix 18 years ago, I had enough curiosity to learn a lot without a ton of help. Of all the skills that will further an IT career, curiosity, for me is the most important. So if I saw that myawesomealias was set, I would want to know what it was, what it did, why it existed. I find navigating Unix/Linux much easier with a few aliases. But to do my job, I need to understand everything that is running on my servers. – Gandolf989 Jun 14 '18 at 13:00
13

The ~ or ~/ refers to the absolute path of your home directory a.k.a. /home/username.


Additionally, if you try cd ~ or cd ~/ they will both do the same thing; the shortest option being simply cd. All three options take you to your home directory. NOT /home.


Since .bashrc is located in your home directory, you must specify its location by adding the tilde, which allows you to point to home directory from wherever you are and thus access the .bashrc.

Of course, this works for any other files and folders located in your ~, for example:

  • cd ~/myFolder
  • ~/myScript.sh

What you were trying to do is open .bashrc, but since vim checks in your current location if the file already exists or not, it will create a new .bashrc file in your current pwd, since there is no current .bashrc where you were trying to open it.

In other words, if you were in /home/username/someFolder/someSubFolder, doing the vim .bashrc command will create a new .bashrc file, since there is no already existing .bashrc and you did not point to the right path, which is /home/username/.bashrc (or ~/.bashrc).

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Jules L
  • 246
  • that was a really helpful and informative answer. I understand it now. – MarkMark Jun 13 '18 at 13:17
  • 3
    The key insight is that /home should not be any account's home directory. It is the parent of most home directories (and the grandparent of Likewise users' home directories, at least in the implementation we use at work, where /home/MYDOMAIN/* is where the home directories for AD user in the MYDOMAIN domain reside). – Monty Harder Jun 13 '18 at 16:39