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

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


    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



Welcome!

It looks like you're new here. Sign in or register to get started.

Comments

  • Registered Users, Registered Users 2 Posts: 14,670 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,729 veryangryman
    ✭✭✭


    Thanks guys got sorted

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

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



Welcome!

It looks like you're new here. Sign in or register to get started.
Advertisement