0

I had created a following code:

function file_name {

  if [ -n $1 ]; then
    parts=$(echo -e $1 | tr "/")

    for a in ${parts}; do
       echo $a
    done
  fi
}

file_name("this_is/a_test/string")

When I run it, I get following error:

./test: line 138: syntax error near unexpected token
 `"this_is/a_test/string"'

./test: line 138: `file_name("this_is/a_test/string")'
2
  • parenthesis are use to create a subshell. Commented Aug 23, 2021 at 12:13
  • Once you've removed the parentheses, run your script through shellcheck.net for other useful corrections. Commented Aug 23, 2021 at 18:41

3 Answers 3

1

Pure bash version without echo, tr and pipe.

Work fine with filenames containing spaces.

#! /bin/bash

function file_name {
    local str="$1"
    local token=
    while [[ "${str}" =~ / ]]; do
        token="${str%%/*}"
        #if [[ "${token}" != "" ]]; then
        echo "${token}"
        #fi
        str="${str#*/}"
    done
    if [[ -n "${str}" ]]; then
        echo "${str}"
    fi
}

file_name "this_is/a_test/string"

Remark:

  • Uncomment if [[ "${token}" != "" ]]; then and fi if you want to squeeze empty names provoked by double slashes (//) or by a path who start by a slash
Sign up to request clarification or add additional context in comments.

Comments

1
  1. bash function calls does not require ()
  2. tr needs a second argument tr "/" " "

Since you don't have any whitespaces, I'd recommend using command substitution to split the string:

function file_name {
    if [ -n $1 ]; then
        for part in ${1//\// } ; do 
            echo "$part"
        done
    fi
}

file_name "this_is/a_test/string"
this_is
a_test
string

Try it online!

1 Comment

Hello. Nice code, but as you wrote, it doesn't allow filenames with spaces, with could case problems by files may contain them. Any ideas to fix it ?
0
function file_name {

  if [ -n $1 ]; then
    parts=$(echo -e $1 | tr "/" " ")

    for a in ${parts}; do
       echo $a
    done
  fi
}

file_name "this_is/a_test/string"

# writes:
# this_is                                                                                                  
# a_test                                                                                                   
# string 

There are two problems in your code. The first is what you are seeing in the error message right now. The reason is that the function is not called properly. Bash handles functions like mini scripts so the syntax for passing arguments to functions is like you pass arguments to scripts.

The second error which you will see after this is corrected is a missing argument to tr. I assumed you want to replace / with a space for the for loop to work properly later.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.