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 ...
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 ...
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) ...
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 ...
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. ...
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,...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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):
...
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 ...
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 ...
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 ...
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 ...
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>...
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 ...
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'...
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 ...
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
...
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 ...
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 ...
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 ...
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.
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
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
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 ~. ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
tilde × 30bash × 19
shell × 6
home × 5
shell-script × 4
ubuntu × 3
variable × 3
quoting × 3
zsh × 2
sudo × 2
filenames × 2
path × 2
cp × 2
variable-substitution × 2
linux × 1
ssh × 1
scripting × 1
filesystems × 1
find × 1
directory × 1
users × 1
environment-variables × 1
keyboard × 1
wildcards × 1
rm × 1