4
kobenz
7d

Today I've had the pleasure of wrapping my brain around shell-quoting in all its insane glory

Comments
  • 5
    Cool! But it's quite straightforward..

    - ´ -- legacy af, never use them
    - ' -- shell won't touch anything inside, ie you will have what you have. No interpolation, no nothing
    - " -- shell will interpolate whatever shell expressions it finds inside

    there's not much more to it than that...
  • 1
    @netikras that's true, but he probably needs to swap between ' and " to nest some strings or smth. That can be annoying.

    Just use some shlex! Python:

    import shlex

    In [4]: shlex.quote("HHa 'aaaa' \"aaa\" ")

    Out[4]: '\'HHa \'"\'"\'aaaa\'"\'"\' "aaa" \''
  • 1
    i like that
  • 1
    @retoor babe can we have schlex

    (that was a joke)
  • 1
    @netikras what do «» quotes do?
  • 1
    @kiki not quotes. Angle brackets. They are stream redirection operators, much like in c++.
  • 1
    @netikras those are Cyrillic кавычки-ёлочки, and I doubt they are part of any C++ spec. Did you mean >>?
  • 3
    @kiki there's a variety of its uses. Single > ensures an empty file and streams stdout into it. Double >> -- ensures a file and does thd same [not necessarily empty, ie O_APPEND -- appends to a gile if it already exists].

    < -- streams file contents into stdin [for some command].

    <<< -- streams following string contents into stdin. So you can compile some input in your script into a ctring and feed itninto some command's stdin.

    << -- allows you to compose a literal multiline string and feed it into stdin; terminated by a customizable terminator [immediately following the << notation]. Also called Heredocs

    <(command) -- translates into a command's stdout file [in the /proc filesystem]. I often use it for ´diff´, as it only accepts files and I sometimes need to diff command outputs
  • 1
    @kiki yes, that's what I meant. I assumed a genuine qn with a typo, not a troll question
  • 1
    @netikras oh come on, this wasn't even trolling 😅 just a bad joke
  • 1
    @kiki in high and low level. Then the bashing starts.
  • 1
    @netikras I do know this stuff but don't use << but use piping to redirect command output to stdin. If I use `cat source.c | ./r --stdin` I can let r review or refactor a whole file based on how to it's configured in .rcontext.txt. With r you can make all sorts of AI applications, ther is a free version that doesn't require an api key right here: https://molodetz.nl/retoor/r. You can make spam filters, review tools and refactor / grammar fix / translation / branding tools in minutes. The possibilities are endless. Does require python 3.14-dev package tho :( Will remove that requirement asap, will do other solution for tooling than python. I assumed that my C application would be very powerful with python plugin but the dev dependency of specific version only causes issues on other people's pc.
  • 0
    You know how ' are used to keep shell from touching the containing string in any way, right?

    What effed me up is that in bash, if you prepend the first apostrophe with a dollar sign, it overrides that and turns on escape sequence translation within the string

    $ echo 'foo\nbar'
    foo\nbar
    $ echo $'foo\nbar'
    foo
    bar
  • 2
    @netikras (stuff...) literally means "run wtv the fucks in there" , { stuff... } does the same with the added "don't you touch my shit"
  • 1
    @retoor I usually do `<c-r>=lua string.format("%q", [[some 'nasty"stringfuck/\]])`
  • 0
    Though 'read' instead of "run" fits best
  • 1
    @kobenz not just that. () runs in a subshell, meaning once you leave (), any variables or whatever you set/export inside parentheses will no longer be available outside them.

    {} is a simple code block with full access to 'parent' scope/vars/envs. {} is just a convenience allowing you to run multiple commands/subscript as one unit and treat it as such [pipe stdout/err/in, handle exit code]
  • 0
    @kobenz why do you say {} is 'dont touch my shit'? I'd say it's the other way around, unless we mean different things
  • 0
    @netikras cuz `()` is an actual operator not just syntax
  • 1
    @netikras learned a bash thing again. You're just spreading the word of hash huh?
  • 1
    @retoor I guess... :)

    using subshells I have implemented multithreading in bash. It's a powerful tool. So why not...
  • 1
    @netikras Implement promishes in bash :P If IIRC someone made a web server that performed quite good in bash. I think you're capable of that too based on your source. Point is, bash seemed to be fast in it.
  • 1
    @retoor I have a webserver built in bash. Though it's single-threaded, didn't invest more time in it :)
  • 1
  • 1
    @netikras I do, I love creative homebrew stuff. I normally do like to do some review but i'm not good enough in bash. Does coding preferences for it even exist?
  • 0
    @retoor shellcheck?

    Though I don't use it.

    And I've developed my own scripting style over time that works very well for my needs
  • 1
    @netikras holy pony, you use namespaces? That's a thing?
  • 1
    @retoor yupp. Useful for libs. Function names can contain :, it makes scripts much more maintainable as you know which lib owns that fn
Add Comment