Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

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

  • 29-07-2022 03:10PM
    #1
    Registered Users, Registered Users 2 Posts: 5,762 ✭✭✭


    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, Registered Users 2, Paid Member Posts: 15,044 ✭✭✭✭Igotadose


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



  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


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

    Works here



  • Registered Users, Registered Users 2 Posts: 5,762 ✭✭✭veryangryman


    Thanks guys got sorted

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

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



Advertisement