Solved

correct expansion of ~ char in a bash script

Posted on 2001-08-24
7
215 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Software required to mount Linux LVM Volume 6 391
Optimize bash script 20 464
what does "puppet agent -t" do ? 2 225
Issues with Ports in Linux (Hyper-V Guest) 2 100
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 gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

816 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

8 Experts available now in Live!

Get 1:1 Help Now