Solved

correct expansion of ~ char in a bash script

Posted on 2001-08-24
7
218 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
[X]
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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
Linux Server is saturating the IP table.. 14 375
Linux : Booting Error after restoring image from OLD PC 23 95
How to ssh into my virtual box linux machine 4 172
Linux Scripting 3 188
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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