How to run a command with a different working directory?2019 Community Moderator Electioncd to original location when recording session with “script” commandrun command as different nologin userchroot with working directory specifiedHow does Unix keep track of a user's working directory when navigating the file system?In what cases may some programs require you to run them on a specific directory?sudo start login shell (-i) and stay at current working directoryhow to run shell command inside awkHow to change working directory of a child process by posix_spawn?tmux change default working directory of a session without attachingComparing working directory with “string”

How to get directions in deep space?

Error in master's thesis, I do not know what to do

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?

Asserting that Atheism and Theism are both faith based positions

Checking @@ROWCOUNT failing

"Marked down as someone wanting to sell shares." What does that mean?

How do I lift the insulation blower into the attic?

Why would five hundred and five same as one?

Should I be concerned about student access to a test bank?

How to split IPA spelling into syllables

How do you say "Trust your struggle." in French?

What properties make a magic weapon befit a Rogue more than a DEX-based Fighter?

How to test the sharpness of a knife?

Do I have to take mana from my deck or hand when tapping this card?

Should a narrator ever describe things based on a character's view instead of facts?

What is the meaning of "You've never met a graph you didn't like?"

What do the positive and negative (+/-) transmit and receive pins mean on Ethernet cables?

"Oh no!" in Latin

Why doesn't Gödel's incompleteness theorem apply to false statements?

How would a solely written language work mechanically

Is there any common country to visit for persons holding UK and Schengen visas?

I keep switching characters, how do I stop?

Amorphous proper classes in MK

Sort with assumptions



How to run a command with a different working directory?



2019 Community Moderator Electioncd to original location when recording session with “script” commandrun command as different nologin userchroot with working directory specifiedHow does Unix keep track of a user's working directory when navigating the file system?In what cases may some programs require you to run them on a specific directory?sudo start login shell (-i) and stay at current working directoryhow to run shell command inside awkHow to change working directory of a child process by posix_spawn?tmux change default working directory of a session without attachingComparing working directory with “string”










0















I use this shell pipeline to get a SQL dump using the terminal:



$ cd var/lib/mysql && mysqldump -uroot -p"craft" --add-drop-table craft > ~/../docker-entrypoint-initdb.d/base.sql && cd ~/..


As can be seen, I entered the var/lib/mysql directory and create the dump to a file and come back from where I was initially.



The command is correct, but, I guess it can be written concisely like without entering directly the var/lib/mysql directory.



Can anyone suggest that?










share|improve this question
























  • More generally ( cd some_place && do_stuff ) Note the parenthesis.

    – ctrl-alt-delor
    14 hours ago
















0















I use this shell pipeline to get a SQL dump using the terminal:



$ cd var/lib/mysql && mysqldump -uroot -p"craft" --add-drop-table craft > ~/../docker-entrypoint-initdb.d/base.sql && cd ~/..


As can be seen, I entered the var/lib/mysql directory and create the dump to a file and come back from where I was initially.



The command is correct, but, I guess it can be written concisely like without entering directly the var/lib/mysql directory.



Can anyone suggest that?










share|improve this question
























  • More generally ( cd some_place && do_stuff ) Note the parenthesis.

    – ctrl-alt-delor
    14 hours ago














0












0








0








I use this shell pipeline to get a SQL dump using the terminal:



$ cd var/lib/mysql && mysqldump -uroot -p"craft" --add-drop-table craft > ~/../docker-entrypoint-initdb.d/base.sql && cd ~/..


As can be seen, I entered the var/lib/mysql directory and create the dump to a file and come back from where I was initially.



The command is correct, but, I guess it can be written concisely like without entering directly the var/lib/mysql directory.



Can anyone suggest that?










share|improve this question
















I use this shell pipeline to get a SQL dump using the terminal:



$ cd var/lib/mysql && mysqldump -uroot -p"craft" --add-drop-table craft > ~/../docker-entrypoint-initdb.d/base.sql && cd ~/..


As can be seen, I entered the var/lib/mysql directory and create the dump to a file and come back from where I was initially.



The command is correct, but, I guess it can be written concisely like without entering directly the var/lib/mysql directory.



Can anyone suggest that?







shell pwd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 6 hours ago









Paradox

163111




163111










asked 15 hours ago









ArefeArefe

17611




17611












  • More generally ( cd some_place && do_stuff ) Note the parenthesis.

    – ctrl-alt-delor
    14 hours ago


















  • More generally ( cd some_place && do_stuff ) Note the parenthesis.

    – ctrl-alt-delor
    14 hours ago

















More generally ( cd some_place && do_stuff ) Note the parenthesis.

– ctrl-alt-delor
14 hours ago






More generally ( cd some_place && do_stuff ) Note the parenthesis.

– ctrl-alt-delor
14 hours ago











2 Answers
2






active

oldest

votes


















6














To be honest, I don't see a reason for the two calls to cd at all.



You don't seem to use the directory that you cd into for anything. You give an absolute path for the location of the database dump. If any custom MySQL configuration file is needed, that would be picked up from the user's home directory in any case.



You could therefore, quite likely, just use



mysqldump -uroot -p"craft" --add-drop-table craft 
> ~/../docker-entrypoint-initdb.d/base.sql


regardless of what directory you run that from.






share|improve this answer

























  • Yes, my understanding is not correct that I need to enter the var/lib/mysql to run the command for the dump.

    – Arefe
    15 hours ago


















3














In this case, you don't need to change working directory at all (as answered by Kusalananda). However, if you're dealing with commands that do need a particular working directory, then there are a couple of things to know that will make it easier for you.



Firstly, since cd sets the OLDPWD variable, we can use that to return to the original directory, without having to know its name. Secondly, we probably want to return whether or not the command succeeded, so use ; rather than && there:



cd "$workdir" && somecommand ; cd "$OLDPWD"


That's still unreliable if the first cd fails; to be more robust, we really need



if cd "$workdir" ; then somecommand ; cd "$OLDPWD" ; fi


Even at this point, we're struggling if we need the exit status of somecommand after this.




It's usually best to run the command in a subshell, and change only the subshell's working directory:



( cd "$workdir" && somecommand )


This last approach is what I normally use and recommend, unless you're doing something which isn't possible from a subshell, such as setting variables for subsequent commands.






share|improve this answer




















  • 1





    cd - is even more portable than cd "$OLDPWD"

    – Stéphane Chazelas
    14 hours ago











  • The last approach is also what I normally use.

    – Weijun Zhou
    13 hours ago











  • @Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to /dev/null.

    – Toby Speight
    12 hours ago










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507148%2fhow-to-run-a-command-with-a-different-working-directory%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














To be honest, I don't see a reason for the two calls to cd at all.



You don't seem to use the directory that you cd into for anything. You give an absolute path for the location of the database dump. If any custom MySQL configuration file is needed, that would be picked up from the user's home directory in any case.



You could therefore, quite likely, just use



mysqldump -uroot -p"craft" --add-drop-table craft 
> ~/../docker-entrypoint-initdb.d/base.sql


regardless of what directory you run that from.






share|improve this answer

























  • Yes, my understanding is not correct that I need to enter the var/lib/mysql to run the command for the dump.

    – Arefe
    15 hours ago















6














To be honest, I don't see a reason for the two calls to cd at all.



You don't seem to use the directory that you cd into for anything. You give an absolute path for the location of the database dump. If any custom MySQL configuration file is needed, that would be picked up from the user's home directory in any case.



You could therefore, quite likely, just use



mysqldump -uroot -p"craft" --add-drop-table craft 
> ~/../docker-entrypoint-initdb.d/base.sql


regardless of what directory you run that from.






share|improve this answer

























  • Yes, my understanding is not correct that I need to enter the var/lib/mysql to run the command for the dump.

    – Arefe
    15 hours ago













6












6








6







To be honest, I don't see a reason for the two calls to cd at all.



You don't seem to use the directory that you cd into for anything. You give an absolute path for the location of the database dump. If any custom MySQL configuration file is needed, that would be picked up from the user's home directory in any case.



You could therefore, quite likely, just use



mysqldump -uroot -p"craft" --add-drop-table craft 
> ~/../docker-entrypoint-initdb.d/base.sql


regardless of what directory you run that from.






share|improve this answer















To be honest, I don't see a reason for the two calls to cd at all.



You don't seem to use the directory that you cd into for anything. You give an absolute path for the location of the database dump. If any custom MySQL configuration file is needed, that would be picked up from the user's home directory in any case.



You could therefore, quite likely, just use



mysqldump -uroot -p"craft" --add-drop-table craft 
> ~/../docker-entrypoint-initdb.d/base.sql


regardless of what directory you run that from.







share|improve this answer














share|improve this answer



share|improve this answer








edited 15 hours ago

























answered 15 hours ago









KusalanandaKusalananda

136k17257426




136k17257426












  • Yes, my understanding is not correct that I need to enter the var/lib/mysql to run the command for the dump.

    – Arefe
    15 hours ago

















  • Yes, my understanding is not correct that I need to enter the var/lib/mysql to run the command for the dump.

    – Arefe
    15 hours ago
















Yes, my understanding is not correct that I need to enter the var/lib/mysql to run the command for the dump.

– Arefe
15 hours ago





Yes, my understanding is not correct that I need to enter the var/lib/mysql to run the command for the dump.

– Arefe
15 hours ago













3














In this case, you don't need to change working directory at all (as answered by Kusalananda). However, if you're dealing with commands that do need a particular working directory, then there are a couple of things to know that will make it easier for you.



Firstly, since cd sets the OLDPWD variable, we can use that to return to the original directory, without having to know its name. Secondly, we probably want to return whether or not the command succeeded, so use ; rather than && there:



cd "$workdir" && somecommand ; cd "$OLDPWD"


That's still unreliable if the first cd fails; to be more robust, we really need



if cd "$workdir" ; then somecommand ; cd "$OLDPWD" ; fi


Even at this point, we're struggling if we need the exit status of somecommand after this.




It's usually best to run the command in a subshell, and change only the subshell's working directory:



( cd "$workdir" && somecommand )


This last approach is what I normally use and recommend, unless you're doing something which isn't possible from a subshell, such as setting variables for subsequent commands.






share|improve this answer




















  • 1





    cd - is even more portable than cd "$OLDPWD"

    – Stéphane Chazelas
    14 hours ago











  • The last approach is also what I normally use.

    – Weijun Zhou
    13 hours ago











  • @Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to /dev/null.

    – Toby Speight
    12 hours ago















3














In this case, you don't need to change working directory at all (as answered by Kusalananda). However, if you're dealing with commands that do need a particular working directory, then there are a couple of things to know that will make it easier for you.



Firstly, since cd sets the OLDPWD variable, we can use that to return to the original directory, without having to know its name. Secondly, we probably want to return whether or not the command succeeded, so use ; rather than && there:



cd "$workdir" && somecommand ; cd "$OLDPWD"


That's still unreliable if the first cd fails; to be more robust, we really need



if cd "$workdir" ; then somecommand ; cd "$OLDPWD" ; fi


Even at this point, we're struggling if we need the exit status of somecommand after this.




It's usually best to run the command in a subshell, and change only the subshell's working directory:



( cd "$workdir" && somecommand )


This last approach is what I normally use and recommend, unless you're doing something which isn't possible from a subshell, such as setting variables for subsequent commands.






share|improve this answer




















  • 1





    cd - is even more portable than cd "$OLDPWD"

    – Stéphane Chazelas
    14 hours ago











  • The last approach is also what I normally use.

    – Weijun Zhou
    13 hours ago











  • @Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to /dev/null.

    – Toby Speight
    12 hours ago













3












3








3







In this case, you don't need to change working directory at all (as answered by Kusalananda). However, if you're dealing with commands that do need a particular working directory, then there are a couple of things to know that will make it easier for you.



Firstly, since cd sets the OLDPWD variable, we can use that to return to the original directory, without having to know its name. Secondly, we probably want to return whether or not the command succeeded, so use ; rather than && there:



cd "$workdir" && somecommand ; cd "$OLDPWD"


That's still unreliable if the first cd fails; to be more robust, we really need



if cd "$workdir" ; then somecommand ; cd "$OLDPWD" ; fi


Even at this point, we're struggling if we need the exit status of somecommand after this.




It's usually best to run the command in a subshell, and change only the subshell's working directory:



( cd "$workdir" && somecommand )


This last approach is what I normally use and recommend, unless you're doing something which isn't possible from a subshell, such as setting variables for subsequent commands.






share|improve this answer















In this case, you don't need to change working directory at all (as answered by Kusalananda). However, if you're dealing with commands that do need a particular working directory, then there are a couple of things to know that will make it easier for you.



Firstly, since cd sets the OLDPWD variable, we can use that to return to the original directory, without having to know its name. Secondly, we probably want to return whether or not the command succeeded, so use ; rather than && there:



cd "$workdir" && somecommand ; cd "$OLDPWD"


That's still unreliable if the first cd fails; to be more robust, we really need



if cd "$workdir" ; then somecommand ; cd "$OLDPWD" ; fi


Even at this point, we're struggling if we need the exit status of somecommand after this.




It's usually best to run the command in a subshell, and change only the subshell's working directory:



( cd "$workdir" && somecommand )


This last approach is what I normally use and recommend, unless you're doing something which isn't possible from a subshell, such as setting variables for subsequent commands.







share|improve this answer














share|improve this answer



share|improve this answer








edited 14 hours ago

























answered 14 hours ago









Toby SpeightToby Speight

5,37611132




5,37611132







  • 1





    cd - is even more portable than cd "$OLDPWD"

    – Stéphane Chazelas
    14 hours ago











  • The last approach is also what I normally use.

    – Weijun Zhou
    13 hours ago











  • @Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to /dev/null.

    – Toby Speight
    12 hours ago












  • 1





    cd - is even more portable than cd "$OLDPWD"

    – Stéphane Chazelas
    14 hours ago











  • The last approach is also what I normally use.

    – Weijun Zhou
    13 hours ago











  • @Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to /dev/null.

    – Toby Speight
    12 hours ago







1




1





cd - is even more portable than cd "$OLDPWD"

– Stéphane Chazelas
14 hours ago





cd - is even more portable than cd "$OLDPWD"

– Stéphane Chazelas
14 hours ago













The last approach is also what I normally use.

– Weijun Zhou
13 hours ago





The last approach is also what I normally use.

– Weijun Zhou
13 hours ago













@Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to /dev/null.

– Toby Speight
12 hours ago





@Stéphane, with the caveat that it also (in POSIX, at least) also prints the new working directory, so consider sending its output to /dev/null.

– Toby Speight
12 hours ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507148%2fhow-to-run-a-command-with-a-different-working-directory%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.