How the home alias (~) is it expanded?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How the home alias (~) is it expanded?
# 1  
Old 11-22-2016
How the home alias (~) is it expanded?

Code:
echo $HOME return : /home/user1

Code:
echo ~ return : /home/user1

Code:
My_path=~/bin/"some dir1/some dir2"
kate  "$My_path/some_file"

Kate open a file "~/~/bin/some dir1/some dir2/some_file" which does not exists.

I was expecting kate to open : "~/bin/some dir1/some dir2/some_file"

Any help is welcome.

PS
Quote:
Code:
My_path=/home/user1/bin/"some dir1/some dir2"
kate  "$My_path/some_file"

open the correct file
# 2  
Old 11-22-2016
Without seeing the source for kate, we have no way to guess at what might be going wrong.

What operating system are you using?

What shell are you using?
# 3  
Old 11-22-2016
Quote:
Originally Posted by Don Cragun
Without seeing the source for kate, we have no way to guess at what might be going wrong.

What operating system are you using?

What shell are you using?
Hello.

Shell : bash
Operating system : opensuse leap 42.2
Kate is KDE editor

Another example :
Code:
MY_PATH="~/bin/200_yast_install"
ls $MY_PATH
ls: cannot access '~/bin/200_yast_install': No such file or directory

Code:
MY_PATH=~"/bin/200_yast_install"
ls $MY_PATH
ls: cannot access '~/bin/200_yast_install': No such file or directory

I think is a " problem.

But if one have space in folder name, seems not to be able to use " with ~
# 4  
Old 11-22-2016
Using tilda can be very tricky... Yes " will make the substitution fail.. the same goes for /~ but there is nothing stopping you to use " after the tild e.g.
Code:
 MY_PATH=~"/bin/200_yast_install"

# 5  
Old 11-22-2016
Try
Code:
MY_PATH=~/"bin/200_yast_install"

# 6  
Old 11-22-2016
Quote:
Originally Posted by jcdole
Hello.

Shell : bash
Operating system : opensuse leap 42.2
Kate is KDE editor

Another example :
Code:
MY_PATH="~/bin/200_yast_install"
ls $MY_PATH
ls: cannot access '~/bin/200_yast_install': No such file or directory

When the tilde is quoted, it is a regular character and should not be expanded by the shell into your home directory. Therefore, the shell is behaving as expected for the above example.
Quote:
Code:
MY_PATH=~"/bin/200_yast_install"
ls $MY_PATH
ls: cannot access '~/bin/200_yast_install': No such file or directory

The tilde here still isn't required to be expanded because the / following the ~ is quoted.

Note, however, that the sequences of commands:
Code:
MY_PATH=~/"bin/200_yast_install"
ls "$MY_PATH"

Code:
MY_PATH="$HOME/bin/200_yast_install"
ls "$MY_PATH"

should do what you want to do. (Note the use of double-quotes around the expansion of MY_PATH in the ls command. Without the quotes there, any spaces that had been quoted when you assigned the string to MY_PATH will be parsed by the shell into separate arguments for ls.

Expanding on what RudiC said...
The above rules apply whenever a ~ expansion is performed. In an assignment statement (and only in an assignment statement), any ~ up through and including the next following colon or slash, if there is one in the current word, must be unquoted for ~ expansion to occur. This allows multiple ~s to be expanded when setting the PATH, CDPATH and similar variables. Note also that the general format of a ~ expansion is any one of the three forms: ~logname, ~logname/, and ~logname: which will expand to the login directory for the user named logname on your system (followed by a colon or slash, respectively, in the last two forms). As you already know, if you omit logname in any of these forms the ~ expands to the quoted expansion of $HOME. ~ expansions only occur when ~ is the first character in a word except for the cases stated above in assignment statements.

Quote:
I think is a " problem.

But if one have space in folder name, seems not to be able to use " with ~
# 7  
Old 11-22-2016
I might be overly paranoid here, but using unquoted strings in shell scripts is asking for trouble. In this case the trouble would be that a home directory has a space char in it (which is very ill advised to do in first place, but if i would have gotten a penny for every idiotic decision i have seen on a system during my career i'd be stinking rich).

Here is an alternative approach: the file /etc/passwd contains the user names as well as the home directories and it is world readable. Its format is colon-separated fields forming records separated by newlines. The home directory is the fifth field, the user name the first. Hence:

Code:
#! /bin/bash

pGetUserHome ()
{
local chUser="$1"

grep '^'"$chUser"':' /etc/passwd | cut -d: -f6

return 0
}

# ------ main ()
local usershome=""

usershome="$(pGetUserHome "username")"
if [ -z "$usershome" ] ; then
     echo "user \"username\" does not exist"
else
     echo "home of \"username\" is: $usershome"
fi

exit 0

Similar with every other user attributes (primary group, login shell, ...).

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Wild char getting expanded in echo

Hi All, I am having a issue in a script. I am trying to execute a select * from a scirpt to a database and printing the the sql string. The * in the sqltring is printing all the files in the directory. How to handle it ? .. .. sql="select * from emp" execute ($sql) echo $sql Here my... (6 Replies)
Discussion started by: arunkumar_mca
6 Replies

2. Solaris

zpool hasn't expanded

Hi Guys, I have a raidz zpool that consists of four disks. 2x2TB, 1x1TB and 1x0.75TB. Originally it was only 1x1TB, 3x0.75TB, and I had around 1.7TB of storage capacity. I've just switched out two of the 0.75TB disks for the 2x2TB ones. I did this one at a time and now the resilvering is... (2 Replies)
Discussion started by: rudigarude
2 Replies

3. UNIX for Dummies Questions & Answers

Create alias files (not alias commands)

If one: $ find -name 'some expression' -type f > newfile and then subsequently wants to create an alias file from each pathname the find command retrieved and the > placed within 'newfile', how would one do this? Ideally, the newly created alias files would all be in one directory. I am... (3 Replies)
Discussion started by: Alexander4444
3 Replies

4. Shell Programming and Scripting

cp -p /home/* home/exp/*.date not working please help

:( ---------- Post updated at 01:51 AM ---------- Previous update was at 01:50 AM ---------- Not working ---------- Post updated at 02:04 AM ---------- Previous update was at 01:51 AM ---------- cp -p /home/* home/exp/*.`date` i am using this (4 Replies)
Discussion started by: rishiraaz
4 Replies

5. Solaris

war file not expanded (tomcat solaris)

hi .. my example.war file is not expanded in the webapps folder.. could u guide me why the file is not expanded when i restart tomcat... (1 Reply)
Discussion started by: senkerth
1 Replies

6. Shell Programming and Scripting

List file with variables expanded

Hello, I have this problem. I have script file, e.g. #!/usr/bin/ksh echo $MY_DIR ls -lt $MY_DIR I want to list the script but with MY_DIR variable expanded. E.g. MY_DIR=/abc/xyz (in shell MY_DIR is set) So I want to list the script and see: #!/usr/bin/ksh echo /abc/xyz ls... (6 Replies)
Discussion started by: r1omen
6 Replies

7. UNIX for Advanced & Expert Users

Redirect contained in variable not expanded as expected

Hi all, I have a selection of files that contain several commands that write to a file and are started as background processes similar to below. command_to_run_simulator -simulator_arguments > log_file 2>&1 & command_to_run_simulator -simulator_arguments > log_file 2>&1 &... (2 Replies)
Discussion started by: noose
2 Replies

8. Programming

Macros expanded output of c files

Hi, I want to just expand the macros in a set of c files from a filelist which are in different directories. I don't want the header files included in the c file to be expanded. I have a perl script which does gcc -E <infile> -I <path to search for header files> -imacros <infile> But in... (1 Reply)
Discussion started by: spsenthil
1 Replies
Login or Register to Ask a Question