-1

It is possible to exec at the specific folder, rather than I need to use two commands separately?

e.g.

cd /opt/folderA
exec ....

cd /opt/folderB
exec ....
Howard
  • 2,135
  • 13
  • 48
  • 72
  • The question is not very clear to me. Your problem is that your shell needs to be in a specific folder before executing a command? `exec /opt/folderA/whatever.sh` would not work for you? – faker Aug 25 '13 at 09:12

2 Answers2

1

The only ways to change current working directory for a process is:

a) the running program changes it by running a cd command from within the program itself

b) cd to the directory first and then run the program (inherits the CWD from the environment of the parent shell)

c) manipulate the /proc/[pid]/cwd file. The only way you could do this conceivably is by launching a separate bash shell, determine its pid, change the aforementioned file (a link), and then launch the program from within that bash shell.

So, to answer your question, there's no way around the "cd" command except option (c) which actually involves more steps than simply running "cd" first.

Michael Martinez
  • 2,645
  • 3
  • 24
  • 35
0

If this two commands are different you can add the folder to your $PATH environment.

export PATH=$PATH:folderA:folderB

Or you can use alias

alias fromA=/opt/folderA/command
alias fromB=/opt/folderB/command

after that you can directly use

fromA

Adding this to your .bashrc will make it permanently.

deagh
  • 2,019
  • 5
  • 19
  • 19