Sample Header Ad - 728x90

`pushd` inside a while loop from file contents does not behave the same depending on read method

1 vote
1 answer
44 views
I'm pushing directories to my stack using a while loop that reads the contents of a file. I've tried two approaches that should be equivalent, but they behave different. # Approach 1
bash
export DIRSTACK_SAVEFILE="/path/to/file"
# file contains full paths to folders in each line
while read line ; do
   pushd -n "$line"
done < <(tac $DIRSTACK_SAVEFILE)
Using this, I can later use dir and pushd +n and all the folders are loaded into the stack. # Approach 2
bash
export DIRSTACK_SAVEFILE="/path/to/file"
# file contains full paths to folders in each line
tac $DIRSTACK_SAVEFILE | while read line ; do
   pushd -n "$line"
done
After executing this approach, the directory stack in my shell has no new folders. # Question Why only the 1st approach changes the directory stack in my shell? I've searched to understand how process substitution and pipe work. I think I understand what I read [here](https://unix.stackexchange.com/a/17117) about process substitution but I haven't found any explanation about pipe that helps me understand this behavior. PD.: I'm using GNU bash, version 5.1.8(1)-release (x86_64-redhat-linux-gnu)
Asked by Jorge Ricardo Alonso Fern&#225;ndez (13 rep)
Jun 17, 2024, 09:26 AM
Last activity: Jun 17, 2024, 09:44 AM