Find the answer to your Linux question:
Results 1 to 2 of 2
Sorry I am new here and novice to shell scripting. I hope I am posting in the correct spot. I do use ubuntu and that alone lead me here. Any ...
  1. #1
    Just Joined!
    Join Date
    Jul 2011
    Posts
    1

    Bash Script with variables assigned to a path

    Sorry I am new here and novice to shell scripting. I hope I am posting in the correct spot. I do use ubuntu and that alone lead me here. Any who...

    I am trying to create a file, that has a variable assigned to it, in another directory. For some reason when I run the script it creates the file only in my pwd and I cannot for the life of me figure out why.

    My current script:

    filename=$(date +"%e-%m")
    touch /home/$filename

    My pwd at the moment is Desktop but it does not matter as I have tried from elsewhere. I have tried the touch command in the following ways (some will be obviously incorrect but I was desperate):

    touch /home/${filename}
    touch /home/'$filename'
    touch /home/$(filename)
    touch /home/$"filename"

    and many more as well as the original format listed. Please help I seriously appreciate it.

  2. #2
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,789
    the method that you've used should provide the desired output - creating a file based on date and month in /home.


    sousley-1 ~ # ls /home/
    sdousley
    sousley-1 ~ # filename=$(date +"%e-%m")
    sousley-1 ~ # touch /home/$filename
    sousley-1 ~ # ls /home/
    23-08 sdousley
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...