Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8412009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:34:09+00:00 2026-06-10T00:34:09+00:00

I have a shell script with the following code: dir=sample `mkdir $dir` `cp /home/bhavya/workspace/UnetStack/logs/log-0.txt

  • 0

I have a shell script with the following code:

dir=sample
`mkdir $dir`
`cp /home/bhavya/workspace/UnetStack/logs/log-0.txt $dir/log.txt`
`cd $dir`

In the last line with the cd command in the back quotes, I was not able to cd into the corresponding directory.
But once I removed the back quotes I was able to cd.
What I was wondering is why didn’t the cd work with the back quote?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:34:10+00:00Added an answer on June 10, 2026 at 12:34 am

    When you ran:

    `mkdir $dir`
    

    the shell first ran the command mkdir $dir in a subshell, capturing its (standard) output, and then ran the captured string as a command. Fortunately, the output was empty, so the second step executed nothing.

    When you then ran:

    `cp /home/bhavya/workspace/UnetStack/logs/log-0.txt $dir/log.txt`
    

    the copy was executed in a subshell, and the output was captured and executed. Again, the output was empty, so the second phase of execution did nothing.

    Then you ran:

    `cd $dir`
    

    Once more, the cd operation was run in a subshell, which exited after changing its own current working directory, but without affecting the parent shell (this is Unix, not a DOS .bat command file). As before, the output of the cd command was captured, and executed, but the output was empty so there was nothing to execute.

    Essentially, you don’t use back-quotes as extensively as you are doing.

    It would be sufficient to write:

    dir=sample
    mkdir $dir
    cp /home/bhavya/workspace/UnetStack/logs/log-0.txt $dir/log.txt
    cd $dir
    ...other activity in the new directory...
    

    Note that if this is in a script, then the normal ways of executing a script would still leave the parent shell in the original directory. There are ways to make it affect the original shell — find out about the . command (or, in bash, the source command; that’s easier to search for).

    You normally use back quotes (or, better, the $(...) notation) to capture data. For example:

    gcc_lib_dir=$(dirname $(dirname $(which gcc)))/lib
    

    The innermost command is which gcc; it might yield /usr/gcc/v4.7.1/bin/gcc; the inner dirname then yields /usr/gcc/v4.7.1/bin; the outer dirname yields /usr/gcc/v4.7.1; the appended /lib gives

    gcc_lib_dir=/usr/gcc/v4.7.1/lib
    

    That also shows why $(...) is superior to the back-quote notation:

    gcc_lib_dir=`dirname \`dirname \\\`which gcc\\\`\``/lib
    

    That’s harder to get right, and harder to type!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following shell script that echos output to the log and errors
In a shell script I have the following code: if echo Mr.32 ; then
I have the following code in a shell script. This only seems to work
I have the following shell script code. Here if I'm using NR==$i in gawk
I have a shell script file (run.sh) that contains the following: #!/bin/bash %JAVA_HOME%/bin/java -jar
I have the following shell script with finds all the files and then should
I have the following simple shell script: # get all servers from config sshservers=`node
I have the following shell script registered in my Login Items preferences but it
I have the following code in a script. The problem is That I want
I have the following. A Java process writing logs to the stdout A shell

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.