Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

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?

like image 242
Lidia Avatar asked Dec 19 '22 15:12

Lidia


1 Answers

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.

like image 59
sjsam Avatar answered May 19 '23 02:05

sjsam