The tilde (~) has several uses in Bash and other Unix-like shells:
-
Home Directory: As mentioned,
~represents the current user's home directory.cd ~ # Changes to the home directory -
User Home Directory: You can specify another user's home directory by using
~username.cd ~john # Changes to the home directory of user 'john' -
Path Expansion: It can be used in file paths to simplify navigation.
ls ~/documents # Lists files in the 'documents' folder in your home directory -
In Scripts: When writing scripts,
~can be used to refer to the home directory dynamically, making scripts more portable across different users. -
Combining with Other Paths: You can combine
~with other directory names to create paths relative to your home directory.cp ~/file.txt ~/backup/ # Copies 'file.txt' from home to 'backup' directory in home
If you have any more questions or need further examples, feel free to ask!
