6

I am trying to use the cut command in my bash script. I need to get the name of a rpm package containing 'sys' in its name, without its full path. So I tried this:

packageName=$(find temp/noarch/ -name '*sys*noarch.rpm' | cut -d "\/" -f 3)

I thought this way could return the package's name without the temp/noarch/, but it only tells me.

I know i could just remove the substring "temp\/noarch\/", but i'd like to make it with cut.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Eos
  • 112
  • 1
  • 9

1 Answers1

12

"\/" doesn't expand to /, so cut complains:

cut: the delimiter must be a single character

Use plain / instead:

packageName=$(find temp/noarch/ -name '*sys*noarch.rpm' | cut -d/ -f3)
choroba
  • 231,213
  • 25
  • 204
  • 289
  • Strange thing that linux does not need here to receive a `\/`. Anyway, worked fine, thanks a lot. – Eos Jul 27 '17 at 12:38
  • @Eos: Strange? Where **does** it need a backslash before slash? – choroba Jul 27 '17 at 12:40
  • I am working with files with paths inside. So, when searching for those paths, i was searching `"path\/of\/a\/file\/"`, for the character escaping. I got used to it. My bad. – Eos Jul 27 '17 at 12:47
  • Take a look at this post: https://stackoverflow.com/questions/15783701/which-characters-need-to-be-escaped-in-bash-how-do-we-know-it – codeforester Jul 27 '17 at 19:37
  • How about for ! ... ? – mjs Apr 11 '22 at 20:18