Skip to main content
22 votes
Accepted

Expansion of tilde in zsh

~ is expanded only in a few contexts. POSIX, for the standard sh mandates echo a=~ to output a=~ (while it mandates ~ to be expanded in a=~ alone). zsh however has a magicequalsubst option which you ...
Stéphane Chazelas's user avatar
22 votes

Why do you have to put ~/ before .bashrc when opening the .bashrc file?

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 ...
Stephen Kitt's user avatar
16 votes
Accepted

Why tilde (~) doesn't expand when used with CLI argument starting with dash?

This is a peculiarity of the bash shell described in its manual: Bash also performs tilde expansion on words satisfying the conditions of variable assignments (as described above under PARAMETERS) ...
Kusalananda's user avatar
  • 339k
15 votes
Accepted

Tilde expansion vs. variables in Bash

Your understanding of how tilde works is incomplete. See man bash and search for Tilde Expansion. It begins with (extra newlines and some bolding added by me): If a word begins with an unquoted ...
cas's user avatar
  • 79.2k
13 votes
Accepted

Why do you have to put ~/ before .bashrc when opening the .bashrc file?

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. ...
Jules L's user avatar
  • 246
13 votes
Accepted

Command not found in zsh, but found in bash

If your PATH actually contains ~/.local/bin with the literal tilde character: that won't work. The tilde needs to be expanded to your home directory. For example, any of these lines are correct in zsh,...
Gilles 'SO- stop being evil''s user avatar
11 votes
Accepted

What's the different between with a ~ and without a ~ in a string variable in Bash?

~ is a shortcut for your home directory, but only when it appears at the beginning of a string outside quotes. The beginning of the right-hand side of an assignment operator is the beginning of a ...
Gilles 'SO- stop being evil''s user avatar
9 votes
Accepted

readlink literal vs variable

The shell does the tilde expansion. readlink doesn't. Bash will not expand tilde within quotes. readlink -f $a does not do what you want as tilde expansion happens before variable expansion, i.e. the ...
Kusalananda's user avatar
  • 339k
8 votes
Accepted

How does the tilde expansion work within a shell variable?

The bash shell will not expand ~ when the tilde is part of the result of a variable expansion. The unquoted tilde prefix (~, ~+ or ~username for the current user named username) is only expanded to ...
Kusalananda's user avatar
  • 339k
7 votes

Where is cd ~ located on Ubuntu?

cd ~ takes you to your home directory. By default, the home directory is determined by the information stored in the “passwd” database (typically /etc/passwd on basic desktop systems, but central ...
Stephen Kitt's user avatar
7 votes

Bash script copy file to user's (wildcard) home dir

The tilde expansion doesn't work if the username part is quoted: $ echo ~root ~"root" /root ~root (and it happens before variable expansion anyway.) But since you're already reading passwd in the ...
ilkkachu's user avatar
  • 141k
6 votes

Bash script copy file to user's (wildcard) home dir

That's because tilde expansion happens before variable expansions: The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command ...
Jeff Schaller's user avatar
  • 67.7k
5 votes
Accepted

Why is `mkdir ~root/.ssh` the same thing as `mkdir /root/.ssh`?

~USER is just a shorthand notation for the home directory of user USER. For a normal user, this would typically be /home/USER, but for root, it is typically /root. As for your question whether one ...
Malte Skoruppa's user avatar
5 votes

Where is cd ~ located on Ubuntu?

Usually, the HOME environment variable will tell you what the home directory is set to, for whichever user is logged in. I.e.: echo $HOME However, if the HOME variable is not set (from info bash): ...
Time4Tea's user avatar
  • 2,405
5 votes

How does the tilde expansion work within a shell variable?

In short, you need both a explicit and unquoted ~user/ (or ~/) and that can not be the result of a variable expansion. You need backupfolder=~/'db_backups' to have a successful ls $backupfolder The ...
QuartzCristal's user avatar
5 votes

Ubuntu doesn't translate /home/testuser to ~

The problem is the trailing / character in the LDAP definition of $HOME: getent passwd testuser testuser:*:<uid>:<gid>:Test User:/home/testuser/:/usr/bin/bash You can demonstrate this ...
Chris Davies's user avatar
5 votes
Accepted

How to find files with find tool in system path ($PATH)? Or alternatively, How to specify starting-point directory for find as an expression?

No need to use sudo, as your $PATH must contain directories that you can already access. The ~ character is a built-in shortcut for the home directory for bash and other shells. But it isn't evaluated ...
Chris Davies's user avatar
4 votes

Why do tilde prefixes expand prior to assignment, but braces don't

In your test, {a..c} isn’t expanded because it’s part of a variable assignment, and handled separately: The words that the parser has marked as variable assignments (those preceding the command ...
Stephen Kitt's user avatar
4 votes
Accepted

Bash script copy file to user's (wildcard) home dir

You seem on the ball so I won't spoon-feed you a script, but here are some pointers: You're using awk incorrectly. Try this instead: awk -F: -v uid_min=${UID_MIN:-1000} '$3%2==0 && $3>...
user1404316's user avatar
  • 3,098
4 votes

how to set tilde + alias style?

Very easy, Create dummy entries in the /etc/passwd file with directory names, as new user names, and append the right directory you want it to point to. That's it. NOTE: Use different/unused ...
Sun's user avatar
  • 189
4 votes

How to find files with find tool in system path ($PATH)? Or alternatively, How to specify starting-point directory for find as an expression?

Your $PATH does contain a ~/bin literally, so it will only find executables in a bin subdirectory of a directory called ~ literally in the current working directory, like after you run mkdir -p '~/bin'...
Stéphane Chazelas's user avatar
3 votes

I accidentally exited vim with :x!~ and now my ~ shows up in a different path

cd /etc/shinken/services rm ./~ Not much to say about that really. By specifying ./ in front of ~ you stop the shell from performing tilde expansion, and it will be treated as the name of a file in ...
Kusalananda's user avatar
  • 339k
3 votes
Accepted

chmod 400 command returning "No such file or directory" error. Trying to launch an instance through AWS

Your tilde expansion is not pointing your current working directory, where the file is. Use either an explicit or the correct tilde-expanded path: chmod 400 /home/crane/Downloads/mykey.pem or ...
DopeGhoti's user avatar
  • 76.9k
3 votes
Accepted

How to save a path with ~ into a variable?

In bash, ash, mksh and yash tilde expansion occurs before parameter expansion, so that can't work. You can use ksh93 or zsh instead here, or resort to eval: user=username # making sure it's a valid ...
Stéphane Chazelas's user avatar
3 votes

What kind of language construct is the swung dash (tilde, ~)?

When a tilde (~) occurs unquoted, either by itself or as prefix of a username, it undergoes tilde expansion. This is one of the word expansions that the shell does on words which is a type of ...
Kusalananda's user avatar
  • 339k
3 votes
Accepted

Ubuntu doesn't translate /home/testuser to ~

It appears there is an extra slash at the end of the directory path in the LDAP record for the user's home directory, i.e. /home/testuser/ instead of /home/testuser Yes, it is common to add a slash ...
telcoM's user avatar
  • 101k
2 votes
Accepted

Where is cd ~ located on Ubuntu?

Generally on linux it is /home/<your-username>/. You can see the path of the current directory if you enter pwd while you are in it.
DisplayName's user avatar
  • 11.8k
2 votes

Tilde not returning home directory

sudo does not necessarily change the home directory of the invoker. You need to use -H to ensure this sudo -H -u user2 -s Or sudo -u user2 -i See man sudo for details
Chris Davies's user avatar
1 vote

Simple variable assignment: Tilde does not expand in quotes

rcutils_path=~/Admir/bin/gungadin-1.0/rcutils Tilde does not work inside quotes. If Admir is your user name, then avoid the first slash: rcutils_path=~Admir/bin/gungadin-1.0/rcutils
Vlastimil Burián's user avatar
1 vote

How to view ~ (tilde) instead of home directory in bash?

What Bash shows in the prompt depends entirely on what is set in PS1. The prompt escape \w (or \W) would show the current directory (or the last part of it), but with the home directory shown as ~. ...
ilkkachu's user avatar
  • 141k

Only top scored, non community-wiki answers of a minimum length are eligible