Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-24-2006, 06:00 AM   #1
Roger Krowiak
Registered: Oct 2004
Distribution: Slackware
Posts: 33

Rep: Reputation: 15
BASH: cannot enter special-named dir from script

I have created some directories with exotic names, read them from script and then I try to enter them using:

cd "$i"

where in i is stored ine single directory. When the directory name is dash - the script will made jump to the previous working directory. I've found in BASH manual that cd - will move you to the previus directory (from directory stack). I know how to workaround this from command line using cd \- but how can I do it from script using cd "$i"? Thank you for help.
Old 08-24-2006, 07:10 AM   #2
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,487

Rep: Reputation: 627Reputation: 627Reputation: 627Reputation: 627Reputation: 627Reputation: 627
If I remember well you can access or list files starting with a dash with "./-myfile". In your case, it would be "./$i".

If this doesn't work, there is a thread on this forum dealing with such an access problem, but I am not able to find it now.

Old 08-24-2006, 07:14 AM   #3
Registered: May 2001
Posts: 28,895
Blog Entries: 55

Rep: Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356Reputation: 3356
That's right. Since you don't use a full path you mean to say "in the current working directory" or "./-".
Here's an example of a check for it:
function pathCheck() { [ "${i:0:1}" != "/" -a -d "$PWD/$i" ] && i="$PWD/$i"; }

Last edited by unSpawn; 08-24-2006 at 07:21 AM. Reason: //fix
Old 08-24-2006, 07:19 AM   #4
Roger Krowiak
Registered: Oct 2004
Distribution: Slackware
Posts: 33

Original Poster
Rep: Reputation: 15
Thank you both for help, it works now correctly!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script test for empty dir uopjohnson Linux - Software 3 10-07-2005 07:45 PM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 07:08 AM
Testing for enter in bash script bwysocki Programming 8 06-16-2005 10:11 AM
Trying to read named.conf forwarders info in bash script. scottman Programming 4 09-11-2004 10:38 PM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 08:53 AM

All times are GMT -5. The time now is 08:07 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration