1

I'm trying to add a filename:filesfullpath into a hidden file, so every time the user runs the script and calls in a file, the file and full file path would be added into the hidden file, presented in the following way:

filename:filepath
filename:filepath

..

I know how to get the file name and the filepath, but I don't know how to put it all in one line with the colon and into the hidden file.

I have

flink=$(readlink -e $1)
fname=$(basename $flink)

fname":"flink >> .hiddenfile

but obviously that didn't work.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
Nabz
  • 126
  • 2
  • 13

1 Answers1

2

Quote your variables. For example:

flink=$(readlink -e "$1")
fname=$(basename "$flink")

echo "${fname}:${flink}" >> /path/to/.hiddenfile
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • Do you mind explaining why we need to use the quotes and what does ${variable} mean still having trouble knowing when to using that. – Nabz Mar 17 '16 at 19:07
  • @Nabz Failing to use quotes or brackets can cause all sorts of problems with word splitting and variable expansion. You should almost always include them for defensive programming, although experienced programmers may omit them in special use cases where word splitting or quoting are undesirable. For starters, see: [Parameter Expansion](http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html), [Quoting](http://www.gnu.org/software/bash/manual/html_node/Quoting.html), and [Word Splitting](http://www.gnu.org/software/bash/manual/html_node/Word-Splitting.html). – Todd A. Jacobs Mar 17 '16 at 19:36