I have a bunch of bash files, and each have a function and a comment block that describes that function.



# Function Name: test_util
# Description: .....

function test() ....

I have a help function that will be triggered when i pass --help parameter my function. The help is specific for each function name.

Function names contain _ in them, while the filenames have - instead. Example: test-util.bash is the filename while the function that will be passed will be called test_util

The command i execute looks like:

helpers test_util --help

This will execute the test_util function in the location ~/helpers/plugins/test-util.bash


I want to dynamically show the comments attached in each file. What i do is:

1- Construct the correct file name from the passed function name using FILENAME= echo "~/helpers/plugins/${1}.bash" | tr '_' '-' to replace any underscores

2- I extract the comments using cat $FILENAME | grep '#' | cut -c 2- | tail -n +3

Now, my problem is with the cat statement as i am passing the filename as a variable that i pass to the function and i dont seem to get this working.

The command works fine in terminal if i am passing the filename directly to the cat command.

Thanks for any help

  • FILENAME="$(echo ... | tr ...)" surely? – AlexP Nov 23 '16 at 18:20
  • @AlexP well it works .. anything wrong in that ? – AhmadAssaf Nov 23 '16 at 18:23
  • Ah I see, you had backticks which got eaten by the formatting... Try adding an echo FILENAME="\"$FILENAME\"" just before the cat command, to see what the value of FILENAME is. – AlexP Nov 23 '16 at 18:29
  • @AlexP i already did and its indeed a correct filepath with the replaced - – AhmadAssaf Nov 23 '16 at 18:33
  • @AlexP actually its not working .. the echo i was getting was from assignment .. it seems that there is something wrong there – AhmadAssaf Nov 23 '16 at 18:41

If your assignment really is

FILENAME= echo "~/helpers/plugins/${1}.bash" | tr '_' '-'

as it appears in the question, then this is incorrect. The correct form is

FILENAME="$(echo "~/helpers/plugins/${1}.bash" | tr '_' '-')"

The command as it appears in the question means to run echo ... | tr ... with an empty variable named FILENAME in the environment.

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.