2

I have a problem with being able to echo a part of a string.

Here is a simplified version:

t=no_1187985:/home/my.name/testing/no

I want to echo no_1187985 only so:

tt=$(echo $t | cut -d":" -f 1)
echo $tt

But this produces just a blank space (and a command no found error in my actual script)

If I changed -f 1 to - f 2, it works for the second field, I don't understand why not the first.

Here is a portion of my actual code for context:

while read line;
    do
        #echo $line
        #sleep 1
        path=$(dirname "$line")
        #echo $path
        #mkdir -p $(echo $path | cut -d":" -f 2)
        inode= $(echo $path | cut -d":" -f 1)
        echo $inode
        #mv ~/deleted/$inode $(echo $line | cut -d":" -f 2);
    done < .temp

This is an attempt to create a recyclebin .temp is a file with the pathnames of different files to 'restore'. the loop looks through each line. It recreates the directory tree before moving the files back into them. the files are currently stored in the deleted folder and have their inode number attached to the end. When the file is 'restored' (i.e moved back), the inode is removed from the filename .

the .temp file contains the file name (the original and inode combined) and the original pathname both are separated by a ":" delimiter.

  • 2
    remove the space : inode=$(echo $path | cut -d":" -f 1) – neuron Aug 23 '15 at 15:40
  • thank you, it seems to work. I dont understand why space is such an issue. – user3120554 Aug 23 '15 at 15:43
8

You are not supposed to have spaces around the = symbol for variable assigments.

Scenario 1:

  • inode = something - bash tries to run a command named inode with 2 arguments.
  • It's equivalent to saying: inode "=" "something"

Scenario 2:

  • inode =something - bash tries to run a command named inode with 1 argument (the string '=something')
  • It's equivalent to saying: inode "=something"

Scenario 3:

  • inode= something - bash tries to run the command something with inode set to the empty string in its environment.
  • It's equivalent to saying: inode="" something

Note: Check section 2.9.1 Simple Commands from the reference provided below.

Reference: Shell Command Language

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.