Solved

correct expansion of ~ char in a bash script

Posted on 2001-08-24
7
213 Views
Last Modified: 2010-04-21
Hello-

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?

Thanks,

Paul T.
0
Comment
Question by:titchener
7 Comments
 
LVL 4

Expert Comment

by:sdussinger
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)....

-----------------------------------------------------------
#!/bin/bash

#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
pwd

#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
pwd

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

HTH

--Steve
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
ID: 6428045
dir="~$1"
eval cd $dir
0
 
LVL 5

Expert Comment

by:paulqna
ID: 6428416
Putting the ~$1 like ~${1} could help. (ksh solution anyway)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

Expert Comment

by:ahoffmann
ID: 6429358
which ksh does this do?
0
 

Author Comment

by:titchener
ID: 6429613
Thanks, that did the job.
0
 
LVL 5

Expert Comment

by:paulqna
ID: 6429929
/bin/ksh ;)

No comment...
0
 
LVL 51

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now