0

we want to exclude some names from variable

# echo $names
abba begiz altonzon music aolala


# echo $names | grep -o '[^[:space:]]\+'
abba
begiz
altonzon
music
aolala

when we use egrep in order to exclude the two names

then we get exception about :"shell-init: error retrieving current directory:"

#  echo $names | grep -o '[^[:space:]]\+' | egrep -iv "abba|begiz"
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
altonzon
music
aolala

how to avoid this exception?

Kusalananda
  • 333,661
yael
  • 13,106

1 Answers1

1

egrep is a shell script in some systems, and at least the shells in Debian don't like starting in a removed directory:

$ mkdir /tmp/z
$ cd /tmp/z
$ rm -r /tmp/z
$ egrep
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

That's with Bash as /bin/sh, Dash gives sh: 0: getcwd() failed: No such file or directory

Use grep -E directly to bypass running that shell script, or don't run in a deleted directory.

Related: What happens when the current directory is deleted?


A completely different matter is that there's probably some better way to do what you're doing, one that doesn't suffer from problems with word splitting and globbing.

In Bash, you could use arrays:

names=(abba begiz altonzon music aolala)
newnames=()
for x in "${names[@]}"; do
    if [[ ! $x =~ ^(abba|begiz)$ ]]; then
        newnames+=("$x")
    fi
done
# do something with newnames
ilkkachu
  • 138,973