LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-18-2014, 12:26 PM   #1
jrs
LQ Newbie
 
Registered: Sep 2014
Posts: 2

Rep: Reputation: Disabled
Question how to save path names in variables in linux terminl


Shell: bash

I'm connecting to a remote linux machine via the shell.

My issue is that I'm constantly going back and forth between a few directories and its getting annoying to type out their names.

I know about tab complete, and I'm trying to explore !!, but right now theyre not as efficient as I want them to be.

I am also aware of the arrow functionality that goes through the command history.

I want to just save the paths in variables like this

myPath="/blah/dee/blah/files"

and then type the command

cd myPath.

Is this not possible, or am i missing something?

Or, how can I do something similar to this using !! or another command?

Last edited by jrs; 09-18-2014 at 12:32 PM. Reason: arrow functionality edit
 
Old 09-18-2014, 12:33 PM   #2
jrs
LQ Newbie
 
Registered: Sep 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
I am also aware of using the arrows to go through my command history
 
Old 09-18-2014, 12:46 PM   #3
rigor
Member
 
Registered: Sep 2011
Posts: 249

Rep: Reputation: Disabled
There are various possibilities, depending on the exact sequence of things in your situation. These include at least:
  1. cd -
  2. pushd / popd
  3. CDPATH

For example, if you are jumping back and forth between two directories A and B which both have the same parent directory, and you start in the parent directory:

Code:
cd A
cd ../B
naturally you would be in directory B.

If you then issue the command:
Code:
cd  -
you will be in directory A. If you issue the same command again, you'll jump back to directory B.

The commands pushd and popd as well as some related commands, will allow you to manipulate a directory stack of sorts, if that could suit your needs.

The CDPATH environment variable can be given a list of directories, much like the PATH variable. But each of the directories in the CDPATH variable can serve as the base for a set of directories which as a sort of shortcut form of the cd command; each of them can be cd'd into just giving the contained directory name to cd.

As an example, suppose a directory named files1 contains directories named A B C and D, a directory named files2 contains directories named V W X Y and Z.

Code:
export CDPATH=/blah/dee/blah/files1:/blah/dee/blah/files2
would allow you do this to go into the contained sub-directories of files1 or files2:

Code:
cd A
cd Z
cd C
cd W

Last edited by rigor; 09-18-2014 at 12:48 PM.
 
1 members found this post helpful.
Old 09-18-2014, 12:58 PM   #4
Habitual
LQ Addict
 
Registered: Jan 2011
Posts: 8,494
Blog Entries: 12

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by jrs View Post
myPath="/blah/dee/blah/files"

and then type the command

cd myPath.
use
Code:
cd "$myPath"
You have to quote variables.
Tip: Quotes are only necessary if there are spaces in the directory names, eg: myPath="/path/to/some directory"

Code:
myPath="/tmp"
echo "$myPath"
/tmp
...
myPath=/tmp
echo "$myPath"
/tmp

but it is not required if there aren't any spaces the directory name. Both could be assumed 'correct' I guess.
 
1 members found this post helpful.
  


Reply


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
oustion about variables(PATH variables) mehrd@d Linux - General 2 08-04-2013 03:32 AM
can save new $PATH variables slinkysteve Linux - Newbie 1 10-29-2003 10:30 AM
Variable names that include variables.... mychl Programming 7 09-24-2003 07:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:27 AM.

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