I have a file /tmp/pathnames
where each line is a pathname to a dir.
/tmp/my dir1
/tmp/dir2
/tmp/dir3
I want to apply the command pushd
to each line in the file, so that I can have those pathnames in the dir stack of the current shell.
Here is a bash script for that purpose:
#! /bin/bash
cat /tmp/pathnames | while read pathname; do
pushd "$pathname"
done
But it doesn't work as I expected, i.e. no stack of dirs is created for my current shell.
Even if I run the commands in the script in the shell directly with source
, it still does not work.
I wonder why and how to solve the problem?
Thanks.
while read
loop, and the same solution fixes it. – Barmar Jan 12 '18 at 16:52< /tmp/pathnames
– Barmar Jan 12 '18 at 16:57source
it in the current shell? @Barmar – Tim Jan 12 '18 at 17:00source
to make the script run in the original shell process. – Barmar Jan 12 '18 at 17:04PATH
, and I want to invoke the script without specifying its pathname. If I have to run it bysource
, is it possible to reuse the pathname inPATH
? – Tim Jan 12 '18 at 17:10source
searchesPATH
by default. If you turn off thesourcepath
option it won't. – Barmar Jan 12 '18 at 17:12