3

In a bash script I am using a variable to hold a path like this:

MY_DIR=/just/a/string/to/my/path

And I want to remove the last two parts of it so it looks like this:

/just/a/string

I am using 'cut' to do it, like this:

echo $MY_DIR | cut -d'/' -f-4

The output is what I expect. Fine. But I want to store in an other variable, like this:

MY_DIR2=$($MY_DIR | cut -d'/' -f-4)

When I execute the script I get the error:

... /just/a/string/to/my/path: No such file or directory

Why is the direct output with echo working, but storing the output in a variable is not?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
JackPOD
  • 43
  • 7
  • BTW, I'd suggest `MY_DIR2=${MY_DIR%/*/*/*}` -- much more efficient than using `sed`. (Also, all-caps variable names are used by the shell and OS -- using lowercase names for your own variables avoids conflicts; see http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, fourth paragraph). – Charles Duffy Nov 14 '17 at 12:47
  • Closed as dupe since the issue is (basically) that while you remembered the need to use `echo` on the standalone command, you forgot it in the command substitution -- so the problem, and the fix, is the same as in the flagged question. – Charles Duffy Nov 14 '17 at 12:57

2 Answers2

4

You need to pass an input string to the shell command using a pipeline in which case cut or any standard shell commands, reads from stdin and acts on it. Some of the ways you can do this are use a pipe-line

dir2=$(echo "$MY_DIR" | cut -d'/' -f-4)

(or) use a here-string which is a shell built-in instead of launching a external shell process

dir2=$(cut -d'/' -f-4 <<< "$MY_DIR")
Inian
  • 80,270
  • 14
  • 142
  • 161
  • 2
    I'm *positive* we've got duplicates for this in the knowledgebase. – Charles Duffy Nov 14 '17 at 12:49
  • @CharlesDuffy: I've done it for tons of similar questions before, but wanted to answer it, because of a decent attempt by someone trying to use the community for a first time – Inian Nov 14 '17 at 13:00
  • In a script, will this also work or not because of the subprocess? my cut is not working. The Here not working as well. – Timo Nov 18 '20 at 19:19
  • @Timo: What is not working? How you are running your code? – Inian Nov 18 '20 at 19:22
  • This was fast; li2=$(tr -d '\n' <<< "$li") running in a file.sh. or: li=$(echo "$li" | tr -d '\n') I want to remove the \n in a line reading a file in bash. – Timo Nov 18 '20 at 19:23
  • What is present in `li`? – Inian Nov 18 '20 at 19:29
  • line ... " another .... [ngClass]="'main tall" --- later I want to replace substrings, it seems I have to switch to powershell or python. Bash is difficult.[here all](https://github.com/tik9/ml/blob/master/solu.sh) – Timo Nov 18 '20 at 19:32
  • 1
    @Timo: I suggest creating a separate question under bash tag for this. You are more likely to get help – Inian Nov 18 '20 at 19:39
-1

Use the grave accent(`) to emulate a command, and use echo too.

MY_DIR2=`echo $MY_DIR | cut -d'/' -f-4`
samthegolden
  • 1,366
  • 1
  • 10
  • 26
  • 1
    This is the old form of command substitution; `$()` is modern POSIX syntax, easier to nest, and doesn't change the way backslashes are interpreted within. – Charles Duffy Nov 14 '17 at 12:46
  • Also, it needs to be `echo "$MY_DIR"`, with the quotes -- otherwise a directory named `~/ * MY IMPORTANT FILES * /` would have the `*`s replaced with names of other directories. – Charles Duffy Nov 14 '17 at 12:55