Announcement

Collapse
No announcement yet.

Unable Set Variable in Bash

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable Set Variable in Bash

    I have to set a variable in bash:

    Code:
    b=$(p | cut -d',' -f4)
    echo $b
    Though, I have to finish up with:

    Code:
    p: command not found
    Is there fundamental bash syntax?

  • #2
    What is p?
    What exactly are you trying to do?

    Comment


    • #3
      p is a variable that I have achieved through a for loop.

      Comment


      • #4
        Try b=$(echo "$p" | cut -d',' -f4)

        Comment


        • #5
          Well, p is not a command, so why try and run it in a subshell like one?
          I think you have to echo in front of it.
          For example:
          b=$(echo "$p" | ... )

          Comment


          • #6
            Use a here string to extend the variable value to p to cut as an alternative of using a pipe-line ( | ) which splits a new sub-shell (avoiding a new process overhead). The <<< is bash exact and not accessible in the POSIX shell sh.

            Code:
            b=$(cut -d',' -f4 <<<"$p")
            echo "$b"
            Moreover double quote () your variables to avoid word-splitting by shell.

            Comment

            Working...
            X