Tilde expansion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tilde expansion
# 1  
Old 11-04-2018
Tilde expansion

(Using Bash 4.4)
When I write something like
Code:
dir="~/dox"
ls $dir

then I get the message that the directory '~/docs' does not exist. I understand that the tilde is not expanded at the time of the above assignment because of the quotes. But why is it not expanded at the time when the ls command is executed?

I also understand that at that time tilde expansion occurs before parameter expansion. But isn't the tilde available for extension at that time? After the initial assignment dir has the value ~docs.

Code:
$ echo $dir
~docs

Then at the time of executing
Code:
ls $dir

that should be the same as writing
Code:
ls ~docs

.What am I missing?
# 2  
Old 11-04-2018
Quote:
Originally Posted by Ralph
(Using Bash 4.4)
When I write something like
Code:
dir="~/dox"
ls $dir

then I get the message that the directory '~/docs' does not exist. I understand that the tilde is not expanded at the time of the above assignment because of the quotes. But why is it not expanded at the time when the ls command is executed?

I also understand that at that time tilde expansion occurs before parameter expansion. But isn't the tilde available for extension at that time? After the initial assignment dir has the value ~docs.

Code:
$ echo $dir
~docs

Then at the time of executing
Code:
ls $dir

that should be the same as writing
Code:
ls ~docs

.What am I missing?
Please carefully proofread your posts before you hit the submit button. Like the shells you will be using, the people who read this forum and try to help you with problems you're having tend to read code VERY literally. So, if you start out with:
Code:
dir="~/dox"

there is absolutely no way that the command:
Code:
echo $dir

is going to produce the output:
Code:
~docs

Instead of that output, it is going to produce the output:
Quote:
~/dox
If we can't run the code you show us and get the same output you say you're getting, it raises all sorts of questions that can waste a lot of time for everyone involved.

Moving on to your other questions...
As you said tilde expansion is performed before parameter expansion (and it is performed by the shell, not by the kernel and not by all of the utilities {such as ls} that you ask the shell to invoke for you). So, if parameter expansion produces output containing a tilde, that tilde won't be expanded.

Note that although ls doesn't perform tilde expansion, there are some commands that you can invoke in the shell that will perform tilde and other expansions. These include, but are not limited to, invoking another shell or invoking eval.

So, to get what you seem to want to do you can use any of the following (and hundreds of other possibilities):
Code:
dir=~/docs
ls "$dir"

Code:
dir=/docs
ls ~"$dir"

Code:
dir=docs
cd
ls "$dir"

Code:
dir=docs
ls "$HOME/$dir"

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-04-2018
I'm sorry for the typo. You're right, the output of the command is ~/docs. I probably should have copy/pasted from the terminal but I extracted the problem from a more complex piece of code and the error occurred during that transcription.

.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use parameter expansion over a parameter expansion in bash.

Hello All, Could you please do help me here as I would like to perform parameter expansion in shell over a parameter expansion. Let's say I have following variable. path="/var/talend/nat/cdc" Now to get only nat I could do following. path1="${path%/*}" path1="${path1##*/}" Here... (8 Replies)
Discussion started by: RavinderSingh13
8 Replies

2. Solaris

Tilde commands not working

Hi I have a script that it sends emails to various recipients using mailx, but some of the recipients should be sent as BCC, but ~b flag when used in the following line, does not put the gmail address as BCC: echo "node $output is down" | mailx -s "$output not pinging" sadf@xyz.com ~b... (8 Replies)
Discussion started by: fretagi
8 Replies

3. Shell Programming and Scripting

Lftp sftp get - script renames the local file with suffix tilde

Hi, Below script used for sftp get, #/bin/bash USER=xxx PASS=xxx HOST=xxx REMOTE_FILE=$1 LOCAL_FILE_LOC=$2 cd $LOCAL_FILE_LOC lftp sftp://$USER:$PASS@$HOST:10022 -e "get $REMOTE_FILE; bye" If file does not exist in sftp server, and file (same as remote file name) exists in local dir,... (4 Replies)
Discussion started by: vhegde1011
4 Replies

4. Solaris

Tilde prefix returns invalid home directory.

I am trying to find the home directory of users on a UNIX (Solaris/AIX) box using echo ~usernameThis does return the home directory for all valid users. For some reason this command also outputs home directory which are non-existent for few users who seem not to have logon access to that... (31 Replies)
Discussion started by: thinkster
31 Replies

5. Shell Programming and Scripting

Disable Tilde Expansion within Bash Scripts?

Does anyone know of a reliable method to disable tilde expansion while a script is running and then re-enable it when the script is done? I often have to work with files that have sections like this in them: 555 ~~~~~~~~~~ AN:AutoAttendant~~~~~~~~~~~~ 9195551212 ~~~~~ ... (1 Reply)
Discussion started by: jaimielives
1 Replies

6. Shell Programming and Scripting

Need to print the expansion of the found string (the expansion is beween two delimiters '-' , '||'

Hi , could anyone help me out with this problem. sample.txt has this content : u001- this is used for project1 || u002- this is used for p2|| not to be printed u003- this is used for project3 || u004- this is used for p4 || u005- this is used for project5 || u006- this is used for p6... (9 Replies)
Discussion started by: Balaji PK
9 Replies

7. Windows & DOS: Issues & Discussions

gVim creates and leaves strange tilde ~ files in Windows

Hey folks, I have used gVim in Windows for many years but I have never found an answer to a very simple question I am going to ask you guys... My question is if there is a way to eliminate the creation of those ~ files that gVim creates in Windows. If you have ever used gVim in Windows you... (2 Replies)
Discussion started by: ghbarratt
2 Replies

8. Shell Programming and Scripting

Delete Duplicate records from a tilde delimited file

Hi All, I want to delete duplicate records from a tilde delimited file. Criteria is considering the first 2 fields, the combination of which has to be unique, below is a sample of records in the input file 1620000010338~2446694087~0~20061130220000~A00BCC1CT... (5 Replies)
Discussion started by: irshadm
5 Replies

9. Shell Programming and Scripting

how to extract a tilde delimited file in unix

i have a file in unix in which datas are like this 07 01 abc data entry Z3 data entry ASSISTANT Z3 39 08 01 POD peadiatrist Z4 POD PeDIATRY Z4 67 01 operator specialist 00 operator UNSPECIFIED A0 00 ... (12 Replies)
Discussion started by: trichyselva
12 Replies

10. Shell Programming and Scripting

replacing commas with tilde in csv file.

hello all, i have a comma delimited file. i want to replace the commas in the file with the tilde symbol using sed. how can i do this? thanks. (4 Replies)
Discussion started by: femig
4 Replies
Login or Register to Ask a Question