correct expansion of ~ char in a bash script

Posted on 2001-08-24
Last Modified: 2010-04-21

I'm writing a simple bash script to back up user accounts on a Linux machine. I want to pass the user name into the script as an argument and then expand it to the full home path for the user. In other words, I want to inside the
script do something like:

cd ~user1

This style works fine in the script. However, I need to be able to pass the user name in as an argument, so I'm trying to do:

cd ~$1

This doesn't expand correctly.

How do I get the desired expansion using the argument?


Paul T.
Question by:titchener
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 6424173
Years ago there used to be a program on Unix called 'glob' which would expand paths like this. It appears that this program has been subsumed into the shell in Linux, so just calling that is not an option. Instead, try these on for size. There are two different ways to do it (1 clean and simple, the other a vicious hack)....


#Version 1
#Extract the home directory for the user from the passwd file.
DIR=`cat /etc/passwd | grep $1 | awk -F: '{print $6}'`
echo Changing to: $DIR
cd $DIR

#Version 2
#This one is a hack. Create a subshell and let it expand the
#arguments for us. After expansion, it changes to the home
#directory, which we print and store back in the parent
#shell. Once the parent shell know the directory to go to
#it can just cd itself.
DIR=`bash -c "cd ~$1;pwd"`
echo Changing to: $DIR
cd $DIR

Both of these approaches work on my machine (Mandrake 7.2 running bash-2.04-12.


LVL 51

Accepted Solution

ahoffmann earned 100 total points
ID: 6428045
eval cd $dir

Expert Comment

ID: 6428416
Putting the ~$1 like ~${1} could help. (ksh solution anyway)
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

LVL 51

Expert Comment

ID: 6429358
which ksh does this do?

Author Comment

ID: 6429613
Thanks, that did the job.

Expert Comment

ID: 6429929
/bin/ksh ;)

No comment...
LVL 51

Expert Comment

ID: 6433246
paulqna, I meant the version, like pdksh, (AT&T) ksh

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question