Search code examples
bashdockerfilecdsubshell

how to resolve warning ''using a subshell to avoid having to cd back'


The folder structure like this:

folder_01
├── folder_02├──Dockerfile_02
├── folder_03├──Dockerfile_03 & example.sh

In the example.sh script, I want to use cd to navigate back to folder_02 and use the dockerfile_02 to build an image and then navigate back to the current dir (which is folder_03) Here's what I tried:

cd ..
cd ./folder_02
docker build . -t image_name

cd ..
cd ./folder_03

In intelliJ, it gave me warning using a subshell to avoid having to cd back, I read some documentation about it but haven't got a solution, can someone help me?


Solution

  • You can invoke the commands in a subshell (...):

    (
       cd ./folder_02
       docker build . -t image_name
    )
    (
       cd ./folder_03
       something else
    )
    

    In scripts that I want to preserve environment I use pushd+popd.

    pushd ./folder_02 >/dev/null
    docker build . -t image_name
    popd >/dev/null
    
    pushd ./folder_03 >/dev/null
    something soemthing
    popd >/dev/null