Visit The New, Modern Unix Linux Community

Pass path variable on SSH

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass path variable on SSH
# 1  
Pass path variable on SSH

hi Gurus,

Have been struggling with this for a while

I have 2 servers , lets say local A and remote B, I need to use both as a part of a pipeline. The folder structure is shared between the two, so I can access the same files and folders from both A and B.

When I try to ssh into B from A, it always goes to my home directory in B , I want to go into a specific folder instead and want to pass the path of the folder as a variable to ssh.

What are my options? Please assist

This is what I tried

[/serverA/]$ workdir1="/My/Intended/Path/"
sshpass -p password ssh << ENDSSH
cd \$workdir1
echo $(pwd)
## Other operations on server B

# 2  
Why doesn't it do what you want / need? Where does it fail?
This User Gave Thanks to RudiC For This Post:
# 3  
The echo statement shows the path of my home directory in B , instead of my intended path. So the variable $workdir1 is not being passes by ssh and the cd is not working.
# 4  
It's easiest to simply add a line of code in your home directory .profile file which changes the directory to the directly you wish after you login.

So, just add this to the bottom of your .profile file :

cd /wherever/you/want/to/be

(that is what I do ... )

This User Gave Thanks to Neo For This Post:
# 5  
I want to go to a different folder for each ssh, adding the line to the profile will fix my ssh dir right?
# 6  
I think the problem lies in the escaping of the $ sign when expanding variables in your here document. Just as a rule of thumb: escape it to use a "remote" variable, don't escape it for using local vars. Having local and remote variables with identical names doesn't help debugging nor understanding what's going on...
# 7  
Originally Posted by senhia83
I want to go to a different folder for each ssh, adding the line to the profile will fix my ssh dir right?
Yes, but for me, I don't want to go to a different directory every time I login because I tend to work on the same project for a day or two Smilie

If I changed directories every time I logged in, I would just ssh in and cd manually.

It's less key stokes and less time to do it that way. If you add a different directory to ssh every time you ssh in, then that is more work (for me at least) than just ssh'ing as normal and cd'ing to where I want to go.

But then again, I like things easy.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #541
Difficulty: Medium
In a statically typed language, a variable cannot be declared before it can be used.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

2. Shell Programming and Scripting

Pass File name and Directory Path through command to python script

I'm writing python script to get the file-names in the current directory and file sizes .I'm able to get file list and their sizes but unable to pass them through command line. I want to use this script to execute on other directory and pass directory path with file name through command line. Any... (1 Reply)
Discussion started by: etldeveloper
1 Replies

3. HP-UX

How to set PATH variable for all HP-UX users when they login using ssh?

Hello friends, I need to set PATH variable for all HP-UX users. I tried to implement it using /etc/profile and /etc/sshrc both none of them work. I don't see sshrc file anywhere. Please advise! TIA (4 Replies)
Discussion started by: prvnrk
4 Replies

4. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

5. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

6. Shell Programming and Scripting

Path a variable to sed that includes a path

Hi I'm trying to select text between two lines, I'm using sed to to this, but I need to pass variables to it. For example start="BEGIN /home/mavkoup/data" end="END" sed -n -e '/${start}/,/${end}/g' doesn't work. I've tried double quotes as well. I think there's a problem with the / in the... (4 Replies)
Discussion started by: mavkoup
4 Replies

7. Shell Programming and Scripting

Appending a path in user's PATH variable

Hello Folks, I want to append a path in user's PATH variable which should be available in current session. Background Numerous persons will run a utility. Aim is to add the absolute path of the utility the first time it runs so that next runs have the PATH in env & users can directly run... (6 Replies)
Discussion started by: vibhor_agarwali
6 Replies

8. Shell Programming and Scripting

one liner to extract path from PATH variable

Hi, Could anyone help me in writing a single line code by either using (sed, awk, perl or whatever) to extract a specific path from the PATH environment variable? for eg: suppose the PATH is being set as follows PATH=/usr/bin/:/usr/local/bin:/bin:/usr/sbin:/usr/bin/java:/usr/bin/perl3.4 ... (2 Replies)
Discussion started by: royalibrahim
2 Replies

9. Shell Programming and Scripting

remove a path from PATH environment variable

Hi I need a script which will remove a path from PATH environment variable. For example $echo PATH /usr/local/bin:/usr/bin:test/rmve:/usr/games $echo rmv test/rmve Here I need a shell script which will remove rmv path (test/rmve) from PATH... (9 Replies)
Discussion started by: madhu84
9 Replies

10. Programming

How to pass FIFO path to client process ?

Hello everybody ! I have a first program, called "server" which build 2 FIFO's in this way: ... #define PERMS 0666 #define FIFO1 "\tmp\cerere" #define FIFO2 "\tmp\raspuns" ... mkfifo(FIFO1, PERMS) mkfifo(FIFO2, PERMS) ... I want to access these FIFO's in a second separate program,... (1 Reply)
Discussion started by: Ametis1970
1 Replies

Featured Tech Videos