Lidia
Lidia

Reputation: 2103

What's wrong with this bash script using cut and sed (cut: command not found)?

I'm getting server and path from an NFS location in bash as follows:

#!/bin/bash

ST="/net/10.111.111.111/path/to/some/dir"
echo $ST
SERVER=$(echo $ST | cut -d'/' -f3)
echo $SERVER
PATH=$(echo $ST | cut -d'/' -f4-)
echo $PATH
PATH=$(echo $ST | cut -d'/' -f4-)
echo $PATH

The same 2 lines are repeated above on purpose. The output is:

/net/10.111.111.111/path/to/some/dir
10.111.111.111
path/to/some/dir
./nn.sh: line 9: cut: command not found

I'm getting what I want but I don't understand why the second call to PATH= produces the above error. What am I missing?

Upvotes: 4

Views: 8120

Answers (1)

sjsam
sjsam

Reputation: 21965

PATH is a system variable which the bash shell uses to find where your binaries(eg cut) are.

So, till :

PATH=$(echo $ST | cut -d'/' -f4-)

things work as expected. But after the command substitution ie $(...), PATH points to a non-standard directory where bash could not find the standard binaries.

So the subsequent command :

PATH=$(echo $ST | cut -d'/' -f4-)

gave you the error :

./nn.sh: line 9: cut: command not found

Moral

Never use uppercase variables for your scripts as they are reserved for the system.

Upvotes: 11

Related Questions