Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Bash: Getting variable directory name for directory which contains spaces and a single quote

Options
  • 29-07-2022 3:10pm
    #1
    Registered Users Posts: 5,553 ✭✭✭


    Here is an example directory

    drwxr-xr-x 1 vangryman 197121 0 Jul 29 13:27 "2022-07-29 13.26.09 Very Angryman's Personal Meeting Room"/


    So i'm trying to get the directory name to later cd into it


    $ cat get_zoom.sh

    #!/bin/bash

    dir=$(ls -tr ../../Downloads/Documents/Zoom/ | tail -1)

    cd "\"\"$dir\"\""

    pwd



    But no joy with how i wrap quotes


    $ sh -x get_zoom.sh

    ++ ls -tr ../../Downloads/Documents/Zoom/

    ++ tail -1

    + dir='2022-07-29 13.28.21 Very Angryman''\''s Personal Meeting Room'

    + cd '""2022-07-29 13.28.21 Very Angryman'\''s Personal Meeting Room""'

    get_zoom.sh: line 3: cd: ""2022-07-29 13.28.21 Very Angryman's Personal Meeting Room"": No such file or directory



Comments

  • Registered Users Posts: 13,447 ✭✭✭✭Igotadose


    Does cd "${dir}" work any better? I think your \"\" wrapper is what's messing it up.



  • Registered Users Posts: 1,109 ✭✭✭Skrynesaver


     cd "$(/bin/ls -tr  ../../Downloads/Documents/Zoom/ | tail -1)"
    

    Works here



  • Registered Users Posts: 5,553 ✭✭✭veryangryman


    Thanks guys got sorted

    zoom_path="/c/Users/$(whoami)/Downloads/Documents/Zoom/"

    cd "${zoom_path}""$(ls -tr ${zoom_path} | tail -1)"



Advertisement