0

I am trying to cut out the prefix from a variable that represents path.

Currently this is my code:

for f in /Users/username/Documents/Dev/beneficiary-service/src/main/helm/*
do
  echo $f
  if [[ $f == 'values'* ]]
  then
      yq d -i $f 'resources.'
  fi
done

I printed $f to see its output. I expected it to be ONLY the filename, without the path (values-stg.yaml). However, this is the output:

+ echo /Users/username/Documents/Dev/beneficiary-service/src/main/helm/values-stg.yaml
/Users/username/Documents/Dev/beneficiary-service/src/main/helm/values-stg.yaml
+ [[ /Users/username/Documents/Dev/beneficiary-service/src/main/helm/values-stg.yaml == \v\a\l\u\e\s* ]]

And also, the "if" statement will never be true, because it considers values* literally as is and not as "anything that starts with values"

2
  • Remove the path using parameter expansion: [[ ${f##*/} == values* ]]. Commented Apr 13, 2021 at 9:10
  • @Dominique Please take a look on my new thread: stackoverflow.com/questions/67089998/… Commented Apr 14, 2021 at 10:34

2 Answers 2

1

Path expansion includes the whole path specified. You can remove it using parameter expansion

[[ ${f##*/} == values* ]]

the "if" statement will never be true

That's not true. Quoting values is not needed, though, as none of the characters is special.

Sign up to request clarification or add additional context in comments.

1 Comment

I took this into docker and got some changes, please take a look on my new thread: stackoverflow.com/questions/67089998/…
0

Use Basename - demonstration below - assigned output of basename "${f}" (in case of white space in file names)

for f in /Users/username/Documents/Dev/beneficiary-service/src/main/helm/*
do
  echo $f
  bf=$(basename "${f}") ; echo ${bf}
  if [[ ${bf} == 'values'* ]]
  then
      echo yq d -i ${bf} 'resources.'
  fi
done

1 Comment

Please take a look in my new thread: stackoverflow.com/questions/67089998/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.