Solved Bash two commands in curly braces?

how is it possible to run these two commands and then truncate them to the end of the curly bracket? thanks

a=g; z="abcdefgh"

echo ${z%%$a*}
error:
echo ${z%%$a:0:4}

echo ${z//ab/01}
error:
echo ${${z//ab/01}:0:4}


How should that look? I wanted to make it as compact as possible and because of the extensive possibilities of the commands in curly brackets it seemed sensible to me to combine two commands and in the latter to separate the output of the first.
 
Code:
z="abcdefgh"; a="fg"; echo ${z:${z%%$a*}0:6}

The part works, but I'm having problems with it all

Code:
std="05.01. one date, 15.06. two date, 25.12. three date"
a=", "; z=$std$a
while [[ $z ]]; do
#    echo ${z%%$a*}

    echo ${z:${z%%$a*}0:6}

    z=${z#*$a}
done
 
Code:
...
while [[ $z ]]; do
...
You are using the double square bracket here. Be a little careful with that. It is a bash-ism (also exists in some Korn shells). The normal square bracket for testing truth values (the "if [ ... ]" construct in shell) is actually done by having an executable whose name is a square bracket (it's literally "/bin/["). The double square bracket command was initially intended to make execution of shell scripts faster, by not having to fork/exec out to an executable. The problem with this is that bash has heavily changed the syntax and semantics of what goes into "[[ ... ]]", so its use is not compatible or portable. To begin with, it is not standardized by Posix. And in the meantime, I think most serious production shells (perhaps excluding things like busybox or educational examples) have changed the implementation to decode the arguments to the single bracket themselves, so using "[[ ... ]]" is no longer faster than the single square bracket.

Now having said that: The double square brackets actually have certain advantages, which is that they don't perform certain variable expansions, and their regular expressions work better. For that reason, I know people who recommend using them instead of single square brackets. But that advice only makes sense if bash is absolutely the only shell you use, and to make sure, you should start every script with "#!/bin/bash" if you want to do that (which doesn't work on FreeBSD).
 
In FreeBSD, if you have bash installed, you can use #!/usr/bin/env bash (note the space before bash) which should also work on other systems.
 
better to avoid bash'isms, you may regret someday having code that is "shell specific"

echo $z | awk 'BEGIN{RS=","}{print} > file
 
I do not understand how the variable can be replaced at the beginning by the result and the curly brace can be defined in a curly bracket.

That's just an example, I don't understand how the variable at the beginning can be exchanged for the result and code in the curly brace can be defined inside a curly brace.

How can I integrate the first fragment of code into the second one without going through the variable specification?
If it's impossible, there is no length limit imposed by numbers can be defined?


Code:
z=abcd; a=d

this both
y=${z%%$a*}
echo ${y:0:-1}

unplace to
${${z%%$a*}:0:-1}
or
${z%%$a*:0:-1}
or
${(${z%%$a*}):0:-1}
 
Thank you, but I need the restriction through numbers so that at the end, for example, only 2 or 4 or 6 digits remain for all variables of different lengths.

Code:
std="05.01.2022 one date, 15.06.2022 two date te, 25.12.2022 three date test"
a=", "; z=$std$a
while [[ $z ]]; do
    nu0=4
    y=${z%%$a*}
    echo ${y:0:nu0}
    z=${z#*$a}
done

05.01.2022 date >> 05.01.2022 or 05.01. or 05.01 or 05. or 05

But I was looking for a way to use the 'y' variable without first add Also define with as code to write inside the following curly bracket.

Code:
    nu0=4
    y=${z%%$a*}
    echo ${y:0:nu0}
to
    echo ${${z%%$a*}:0:nu0}
 
Back
Top