Why doesn't using two cd commands in bash script execute the second command?Bash script: How to execute commands consecutively without waiting for the previous one?sudo bash can't execute scriptBash script doesn't execute command from PATHWhy doesn't this second nohup command run?Why won't this php script execute bash script?Script for Opening Two Terminals and Execute Three Consecutive CommandsHow to execute commands in gnuplot using shell script?FIFO commands in bash scriptRandom script using bashCreate bash script that allows you to choose multiple options instead of just one?

Official degrees of earth’s rotation per day

Would it take an action or something similar to activate the blindsight property of a Dragon Mask?

How to explain that I do not want to visit a country due to personal safety concern?

What's the difference between /ɪ/ and /i(ː)/?

Professor being mistaken for a grad student

Declaring defaulted assignment operator as constexpr: which compiler is right?

What approach do we need to follow for projects without a test environment?

PTIJ: Who should I vote for? (21st Knesset Edition)

Science-fiction short story where space navy wanted hospital ships and settlers had guns mounted everywhere

Recruiter wants very extensive technical details about all of my previous work

What are substitutions for coconut in curry?

How do I hide Chekhov's Gun?

Python: Check if string and its substring are existing in the same list

What do Xenomorphs eat in the Alien series?

Ban on all campaign finance?

Why is the BSI not using powers of two?

Why do Australian milk farmers need to protest supermarkets' milk price?

How to deal with taxi scam when on vacation?

Why doesn't using two cd commands in bash script execute the second command?

Backup with Hanoi Strategy

Know when to turn notes upside-down(eighth notes, sixteen notes, etc.)

Rules about breaking the rules. How do I do it well?

Fantasy series where a Vietnam vet is transported to a fantasy land

Did Ender ever learn that he killed Stilson and/or Bonzo?



Why doesn't using two cd commands in bash script execute the second command?


Bash script: How to execute commands consecutively without waiting for the previous one?sudo bash can't execute scriptBash script doesn't execute command from PATHWhy doesn't this second nohup command run?Why won't this php script execute bash script?Script for Opening Two Terminals and Execute Three Consecutive CommandsHow to execute commands in gnuplot using shell script?FIFO commands in bash scriptRandom script using bashCreate bash script that allows you to choose multiple options instead of just one?













11















I have written a bash script which creates a series of directories and clones a project to selected directories.



For that, I need to cd to each directory (project 1 and project 2), but the script doesn't cd to the second directory nor executes the command.



Instead, it stops after cd and cloning in theproject2 directory. Why doesn't it call the cd_project1 function in the following code?



#!/bin/bash
#Get the current user name

function my_user_name()
current_user=$USER
echo " Current user is $current_user"


#Creating useful directories

function create_useful_directories()
if [[ ! -d "$scratch" ]]; then
echo "creating relevant directory"
mkdir -p /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2
else
echo "scratch directory already exists"
:
fi


#Going to project2 and cloning

function cd_project2()

cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2 &&
git clone https://username@bitbucket.org/teamsinspace/documentation-tests.git
exec bash


#Going to project1 directory and cloning
function cd_project1()

cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/ &&
git clone https://username@bitbucket.org/teamsinspace/documentation-tests.git
exec bash



#Running the functions
function main()

my_user_name
create_useful_directories
cd_project2
cd_project1

main


Terminal output:



~/Downloads$. ./bash_install_script.sh 
Current user is mihi
creating relevant directory
Cloning into 'documentation-tests'...
remote: Counting objects: 125, done.
remote: Compressing objects: 100% (115/115), done.
remote: Total 125 (delta 59), reused 0 (delta 0)
Receiving objects: 100% (125/125), 33.61 KiB | 362.00 KiB/s, done.
Resolving deltas: 100% (59/59), done.
~/Downloads/scratch/mihi/project1/project2$









share|improve this question









New contributor




Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.

    – LeonidMew
    16 hours ago











  • Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.

    – Jenny
    16 hours ago






  • 1





    @Jenny, don't feel rushed. Read What should I do when someone answers my question? instead and act accordingly when you are satisfied. Just take your time, there is no reason to hurry. It's perfectly OK if you decide in a day or in a week or in whatever time it takes.

    – PerlDuck
    16 hours ago






  • 2





    @LeonidMew it's barely been 45 minutes since the question was asked, waiting longer is A-OK, a better answer might even come along (like PerlDuck's comment says, it just popped up while I was typing)

    – Xen2050
    16 hours ago






  • 5





    I'm curious what you intended for the exec bash to do.

    – Dennis Williamson
    15 hours ago















11















I have written a bash script which creates a series of directories and clones a project to selected directories.



For that, I need to cd to each directory (project 1 and project 2), but the script doesn't cd to the second directory nor executes the command.



Instead, it stops after cd and cloning in theproject2 directory. Why doesn't it call the cd_project1 function in the following code?



#!/bin/bash
#Get the current user name

function my_user_name()
current_user=$USER
echo " Current user is $current_user"


#Creating useful directories

function create_useful_directories()
if [[ ! -d "$scratch" ]]; then
echo "creating relevant directory"
mkdir -p /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2
else
echo "scratch directory already exists"
:
fi


#Going to project2 and cloning

function cd_project2()

cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2 &&
git clone https://username@bitbucket.org/teamsinspace/documentation-tests.git
exec bash


#Going to project1 directory and cloning
function cd_project1()

cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/ &&
git clone https://username@bitbucket.org/teamsinspace/documentation-tests.git
exec bash



#Running the functions
function main()

my_user_name
create_useful_directories
cd_project2
cd_project1

main


Terminal output:



~/Downloads$. ./bash_install_script.sh 
Current user is mihi
creating relevant directory
Cloning into 'documentation-tests'...
remote: Counting objects: 125, done.
remote: Compressing objects: 100% (115/115), done.
remote: Total 125 (delta 59), reused 0 (delta 0)
Receiving objects: 100% (125/125), 33.61 KiB | 362.00 KiB/s, done.
Resolving deltas: 100% (59/59), done.
~/Downloads/scratch/mihi/project1/project2$









share|improve this question









New contributor




Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.

    – LeonidMew
    16 hours ago











  • Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.

    – Jenny
    16 hours ago






  • 1





    @Jenny, don't feel rushed. Read What should I do when someone answers my question? instead and act accordingly when you are satisfied. Just take your time, there is no reason to hurry. It's perfectly OK if you decide in a day or in a week or in whatever time it takes.

    – PerlDuck
    16 hours ago






  • 2





    @LeonidMew it's barely been 45 minutes since the question was asked, waiting longer is A-OK, a better answer might even come along (like PerlDuck's comment says, it just popped up while I was typing)

    – Xen2050
    16 hours ago






  • 5





    I'm curious what you intended for the exec bash to do.

    – Dennis Williamson
    15 hours ago













11












11








11








I have written a bash script which creates a series of directories and clones a project to selected directories.



For that, I need to cd to each directory (project 1 and project 2), but the script doesn't cd to the second directory nor executes the command.



Instead, it stops after cd and cloning in theproject2 directory. Why doesn't it call the cd_project1 function in the following code?



#!/bin/bash
#Get the current user name

function my_user_name()
current_user=$USER
echo " Current user is $current_user"


#Creating useful directories

function create_useful_directories()
if [[ ! -d "$scratch" ]]; then
echo "creating relevant directory"
mkdir -p /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2
else
echo "scratch directory already exists"
:
fi


#Going to project2 and cloning

function cd_project2()

cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2 &&
git clone https://username@bitbucket.org/teamsinspace/documentation-tests.git
exec bash


#Going to project1 directory and cloning
function cd_project1()

cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/ &&
git clone https://username@bitbucket.org/teamsinspace/documentation-tests.git
exec bash



#Running the functions
function main()

my_user_name
create_useful_directories
cd_project2
cd_project1

main


Terminal output:



~/Downloads$. ./bash_install_script.sh 
Current user is mihi
creating relevant directory
Cloning into 'documentation-tests'...
remote: Counting objects: 125, done.
remote: Compressing objects: 100% (115/115), done.
remote: Total 125 (delta 59), reused 0 (delta 0)
Receiving objects: 100% (125/125), 33.61 KiB | 362.00 KiB/s, done.
Resolving deltas: 100% (59/59), done.
~/Downloads/scratch/mihi/project1/project2$









share|improve this question









New contributor




Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have written a bash script which creates a series of directories and clones a project to selected directories.



For that, I need to cd to each directory (project 1 and project 2), but the script doesn't cd to the second directory nor executes the command.



Instead, it stops after cd and cloning in theproject2 directory. Why doesn't it call the cd_project1 function in the following code?



#!/bin/bash
#Get the current user name

function my_user_name()
current_user=$USER
echo " Current user is $current_user"


#Creating useful directories

function create_useful_directories()
if [[ ! -d "$scratch" ]]; then
echo "creating relevant directory"
mkdir -p /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2
else
echo "scratch directory already exists"
:
fi


#Going to project2 and cloning

function cd_project2()

cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/project2 &&
git clone https://username@bitbucket.org/teamsinspace/documentation-tests.git
exec bash


#Going to project1 directory and cloning
function cd_project1()

cd /home/"$current_user"/Downloads/scratch/"$current_user"/project1/ &&
git clone https://username@bitbucket.org/teamsinspace/documentation-tests.git
exec bash



#Running the functions
function main()

my_user_name
create_useful_directories
cd_project2
cd_project1

main


Terminal output:



~/Downloads$. ./bash_install_script.sh 
Current user is mihi
creating relevant directory
Cloning into 'documentation-tests'...
remote: Counting objects: 125, done.
remote: Compressing objects: 100% (115/115), done.
remote: Total 125 (delta 59), reused 0 (delta 0)
Receiving objects: 100% (125/125), 33.61 KiB | 362.00 KiB/s, done.
Resolving deltas: 100% (59/59), done.
~/Downloads/scratch/mihi/project1/project2$






bash scripts cd-command






share|improve this question









New contributor




Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









Dan

7,10934573




7,10934573






New contributor




Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 17 hours ago









JennyJenny

564




564




New contributor




Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Jenny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2





    Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.

    – LeonidMew
    16 hours ago











  • Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.

    – Jenny
    16 hours ago






  • 1





    @Jenny, don't feel rushed. Read What should I do when someone answers my question? instead and act accordingly when you are satisfied. Just take your time, there is no reason to hurry. It's perfectly OK if you decide in a day or in a week or in whatever time it takes.

    – PerlDuck
    16 hours ago






  • 2





    @LeonidMew it's barely been 45 minutes since the question was asked, waiting longer is A-OK, a better answer might even come along (like PerlDuck's comment says, it just popped up while I was typing)

    – Xen2050
    16 hours ago






  • 5





    I'm curious what you intended for the exec bash to do.

    – Dennis Williamson
    15 hours ago












  • 2





    Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.

    – LeonidMew
    16 hours ago











  • Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.

    – Jenny
    16 hours ago






  • 1





    @Jenny, don't feel rushed. Read What should I do when someone answers my question? instead and act accordingly when you are satisfied. Just take your time, there is no reason to hurry. It's perfectly OK if you decide in a day or in a week or in whatever time it takes.

    – PerlDuck
    16 hours ago






  • 2





    @LeonidMew it's barely been 45 minutes since the question was asked, waiting longer is A-OK, a better answer might even come along (like PerlDuck's comment says, it just popped up while I was typing)

    – Xen2050
    16 hours ago






  • 5





    I'm curious what you intended for the exec bash to do.

    – Dennis Williamson
    15 hours ago







2




2





Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.

– LeonidMew
16 hours ago





Consider accepting one of the answers. If more than one answer is a solution to a question - accept the best one and up-vote another.

– LeonidMew
16 hours ago













Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.

– Jenny
16 hours ago





Hi LeonidMew. Sorry I have no idea how to accept the answers. Both answers are equally good though.

– Jenny
16 hours ago




1




1





@Jenny, don't feel rushed. Read What should I do when someone answers my question? instead and act accordingly when you are satisfied. Just take your time, there is no reason to hurry. It's perfectly OK if you decide in a day or in a week or in whatever time it takes.

– PerlDuck
16 hours ago





@Jenny, don't feel rushed. Read What should I do when someone answers my question? instead and act accordingly when you are satisfied. Just take your time, there is no reason to hurry. It's perfectly OK if you decide in a day or in a week or in whatever time it takes.

– PerlDuck
16 hours ago




2




2





@LeonidMew it's barely been 45 minutes since the question was asked, waiting longer is A-OK, a better answer might even come along (like PerlDuck's comment says, it just popped up while I was typing)

– Xen2050
16 hours ago





@LeonidMew it's barely been 45 minutes since the question was asked, waiting longer is A-OK, a better answer might even come along (like PerlDuck's comment says, it just popped up while I was typing)

– Xen2050
16 hours ago




5




5





I'm curious what you intended for the exec bash to do.

– Dennis Williamson
15 hours ago





I'm curious what you intended for the exec bash to do.

– Dennis Williamson
15 hours ago










3 Answers
3






active

oldest

votes


















19














The culprits are your exec bash statements in some of your functions.
The exec statement is a bit weird and not easily understood in the first place.
It means: execute the following command instead of the currently running
command/shell/script from here on
. That is: it replaces the current shell
script (in your case) with an instance of bash and it never returns.



You can try this out with a shell and issue



exec sleep 5


This will replace your current shell (the bash) with the command sleep 5
and when that command returns (after 5 seconds) your window will close because
the shell has been replaced with sleep 5.



Same with your script: If you put exec something into your script, the script
gets replaced with something and when that something stops execution, the
whole script stops.



Simply dropping the exec bash statements should do.






share|improve this answer


















  • 1





    @Jenny Nice to hear. Anecdote: The Perl language also has an exec statement with the same behaviour and if you put some statements after an exec statement (like exec something; print "This won't run";) then Perl will warn you that the print statement will never get executed.

    – PerlDuck
    16 hours ago






  • 2





    BTW congrats on using && after cd, (if you don’t use set -e). I have seen Code like cd tmp; rm -rf * fail horrible

    – eckes
    14 hours ago


















11














From help exec:




exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
Replace the shell with the given command.

Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.



The key word here is replace - if you exec bash from inside a script, no further script execution can occur.






share|improve this answer






























    2














    if you want a return to the directory you started you could use



    cd -


    But if you are not sure whether a cd command was executed at all it would be better to use the commands for putting working directories onto a stack:



    pushd


    and return to it (even after multiple directory changes)



    popd


    be sure to have equaly pushd and popd commands.






    share|improve this answer























    • I'm not sure you read or understood the problem space. None of these commands will help the user.

      – pipe
      1 hour ago










    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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
    );



    );






    Jenny is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1125755%2fwhy-doesnt-using-two-cd-commands-in-bash-script-execute-the-second-command%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    19














    The culprits are your exec bash statements in some of your functions.
    The exec statement is a bit weird and not easily understood in the first place.
    It means: execute the following command instead of the currently running
    command/shell/script from here on
    . That is: it replaces the current shell
    script (in your case) with an instance of bash and it never returns.



    You can try this out with a shell and issue



    exec sleep 5


    This will replace your current shell (the bash) with the command sleep 5
    and when that command returns (after 5 seconds) your window will close because
    the shell has been replaced with sleep 5.



    Same with your script: If you put exec something into your script, the script
    gets replaced with something and when that something stops execution, the
    whole script stops.



    Simply dropping the exec bash statements should do.






    share|improve this answer


















    • 1





      @Jenny Nice to hear. Anecdote: The Perl language also has an exec statement with the same behaviour and if you put some statements after an exec statement (like exec something; print "This won't run";) then Perl will warn you that the print statement will never get executed.

      – PerlDuck
      16 hours ago






    • 2





      BTW congrats on using && after cd, (if you don’t use set -e). I have seen Code like cd tmp; rm -rf * fail horrible

      – eckes
      14 hours ago















    19














    The culprits are your exec bash statements in some of your functions.
    The exec statement is a bit weird and not easily understood in the first place.
    It means: execute the following command instead of the currently running
    command/shell/script from here on
    . That is: it replaces the current shell
    script (in your case) with an instance of bash and it never returns.



    You can try this out with a shell and issue



    exec sleep 5


    This will replace your current shell (the bash) with the command sleep 5
    and when that command returns (after 5 seconds) your window will close because
    the shell has been replaced with sleep 5.



    Same with your script: If you put exec something into your script, the script
    gets replaced with something and when that something stops execution, the
    whole script stops.



    Simply dropping the exec bash statements should do.






    share|improve this answer


















    • 1





      @Jenny Nice to hear. Anecdote: The Perl language also has an exec statement with the same behaviour and if you put some statements after an exec statement (like exec something; print "This won't run";) then Perl will warn you that the print statement will never get executed.

      – PerlDuck
      16 hours ago






    • 2





      BTW congrats on using && after cd, (if you don’t use set -e). I have seen Code like cd tmp; rm -rf * fail horrible

      – eckes
      14 hours ago













    19












    19








    19







    The culprits are your exec bash statements in some of your functions.
    The exec statement is a bit weird and not easily understood in the first place.
    It means: execute the following command instead of the currently running
    command/shell/script from here on
    . That is: it replaces the current shell
    script (in your case) with an instance of bash and it never returns.



    You can try this out with a shell and issue



    exec sleep 5


    This will replace your current shell (the bash) with the command sleep 5
    and when that command returns (after 5 seconds) your window will close because
    the shell has been replaced with sleep 5.



    Same with your script: If you put exec something into your script, the script
    gets replaced with something and when that something stops execution, the
    whole script stops.



    Simply dropping the exec bash statements should do.






    share|improve this answer













    The culprits are your exec bash statements in some of your functions.
    The exec statement is a bit weird and not easily understood in the first place.
    It means: execute the following command instead of the currently running
    command/shell/script from here on
    . That is: it replaces the current shell
    script (in your case) with an instance of bash and it never returns.



    You can try this out with a shell and issue



    exec sleep 5


    This will replace your current shell (the bash) with the command sleep 5
    and when that command returns (after 5 seconds) your window will close because
    the shell has been replaced with sleep 5.



    Same with your script: If you put exec something into your script, the script
    gets replaced with something and when that something stops execution, the
    whole script stops.



    Simply dropping the exec bash statements should do.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 17 hours ago









    PerlDuckPerlDuck

    7,12611536




    7,12611536







    • 1





      @Jenny Nice to hear. Anecdote: The Perl language also has an exec statement with the same behaviour and if you put some statements after an exec statement (like exec something; print "This won't run";) then Perl will warn you that the print statement will never get executed.

      – PerlDuck
      16 hours ago






    • 2





      BTW congrats on using && after cd, (if you don’t use set -e). I have seen Code like cd tmp; rm -rf * fail horrible

      – eckes
      14 hours ago












    • 1





      @Jenny Nice to hear. Anecdote: The Perl language also has an exec statement with the same behaviour and if you put some statements after an exec statement (like exec something; print "This won't run";) then Perl will warn you that the print statement will never get executed.

      – PerlDuck
      16 hours ago






    • 2





      BTW congrats on using && after cd, (if you don’t use set -e). I have seen Code like cd tmp; rm -rf * fail horrible

      – eckes
      14 hours ago







    1




    1





    @Jenny Nice to hear. Anecdote: The Perl language also has an exec statement with the same behaviour and if you put some statements after an exec statement (like exec something; print "This won't run";) then Perl will warn you that the print statement will never get executed.

    – PerlDuck
    16 hours ago





    @Jenny Nice to hear. Anecdote: The Perl language also has an exec statement with the same behaviour and if you put some statements after an exec statement (like exec something; print "This won't run";) then Perl will warn you that the print statement will never get executed.

    – PerlDuck
    16 hours ago




    2




    2





    BTW congrats on using && after cd, (if you don’t use set -e). I have seen Code like cd tmp; rm -rf * fail horrible

    – eckes
    14 hours ago





    BTW congrats on using && after cd, (if you don’t use set -e). I have seen Code like cd tmp; rm -rf * fail horrible

    – eckes
    14 hours ago













    11














    From help exec:




    exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
    Replace the shell with the given command.

    Execute COMMAND, replacing this shell with the specified program.
    ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
    any redirections take effect in the current shell.



    The key word here is replace - if you exec bash from inside a script, no further script execution can occur.






    share|improve this answer



























      11














      From help exec:




      exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
      Replace the shell with the given command.

      Execute COMMAND, replacing this shell with the specified program.
      ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
      any redirections take effect in the current shell.



      The key word here is replace - if you exec bash from inside a script, no further script execution can occur.






      share|improve this answer

























        11












        11








        11







        From help exec:




        exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
        Replace the shell with the given command.

        Execute COMMAND, replacing this shell with the specified program.
        ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
        any redirections take effect in the current shell.



        The key word here is replace - if you exec bash from inside a script, no further script execution can occur.






        share|improve this answer













        From help exec:




        exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
        Replace the shell with the given command.

        Execute COMMAND, replacing this shell with the specified program.
        ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
        any redirections take effect in the current shell.



        The key word here is replace - if you exec bash from inside a script, no further script execution can occur.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 17 hours ago









        steeldriversteeldriver

        69.3k11114186




        69.3k11114186





















            2














            if you want a return to the directory you started you could use



            cd -


            But if you are not sure whether a cd command was executed at all it would be better to use the commands for putting working directories onto a stack:



            pushd


            and return to it (even after multiple directory changes)



            popd


            be sure to have equaly pushd and popd commands.






            share|improve this answer























            • I'm not sure you read or understood the problem space. None of these commands will help the user.

              – pipe
              1 hour ago















            2














            if you want a return to the directory you started you could use



            cd -


            But if you are not sure whether a cd command was executed at all it would be better to use the commands for putting working directories onto a stack:



            pushd


            and return to it (even after multiple directory changes)



            popd


            be sure to have equaly pushd and popd commands.






            share|improve this answer























            • I'm not sure you read or understood the problem space. None of these commands will help the user.

              – pipe
              1 hour ago













            2












            2








            2







            if you want a return to the directory you started you could use



            cd -


            But if you are not sure whether a cd command was executed at all it would be better to use the commands for putting working directories onto a stack:



            pushd


            and return to it (even after multiple directory changes)



            popd


            be sure to have equaly pushd and popd commands.






            share|improve this answer













            if you want a return to the directory you started you could use



            cd -


            But if you are not sure whether a cd command was executed at all it would be better to use the commands for putting working directories onto a stack:



            pushd


            and return to it (even after multiple directory changes)



            popd


            be sure to have equaly pushd and popd commands.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 7 hours ago









            Bernd Wilke πφBernd Wilke πφ

            1265




            1265












            • I'm not sure you read or understood the problem space. None of these commands will help the user.

              – pipe
              1 hour ago

















            • I'm not sure you read or understood the problem space. None of these commands will help the user.

              – pipe
              1 hour ago
















            I'm not sure you read or understood the problem space. None of these commands will help the user.

            – pipe
            1 hour ago





            I'm not sure you read or understood the problem space. None of these commands will help the user.

            – pipe
            1 hour ago










            Jenny is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            Jenny is a new contributor. Be nice, and check out our Code of Conduct.












            Jenny is a new contributor. Be nice, and check out our Code of Conduct.











            Jenny is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1125755%2fwhy-doesnt-using-two-cd-commands-in-bash-script-execute-the-second-command%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

            Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

            Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

            Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?