How to insert string (or character) into another string at specified index position from the end in bash?

Here implementation of similar function but for which index position is specified from the start of the string.

How looks implementation of general function for which index position can be specified from the end of the string?

Examples with input parameters and expected results for the required function / procedure:

Example:

string: "2278"
string_to_insert: "."

# After inserting "." to 3-rd index position from the end of "string" result should be:
expected_result="2.278"

Related question:

  • How to insert string (or character) into another string at specified index position in bash?

( This question will be self-answered.

This question is part of the project related to my another question: How to retrieve text from the current line at specified cursor position before and after up to specified boundary characters? )


[ Note for gurus. If you want to mark this question and my answer with downvote you of course can do this, but please describe the reason in the comment. I’m always interesting in such feedbacks. And I promise not to follow you with “downvotes revenge”.
Why I am adding such meta note you can understand by reading my post on the topic of “research effort” for questions. ]

cat insert_string.sh

#!/bin/bash

insert_at_position_from_end() {
    local string=$1; shift
    local string_to_insert=$1; shift
    local index=$1; shift
    declare -n out_result=$1; shift

    local length=${#string}

    if (( $index < 0 || $index > $length )); then
        error_msg="index out of bounds error: index=$index, string length=$length; expected indexes: [0..$length]" 
        out_result=""
        echo "$error_msg" >&2
        return 2  # exit 2 # for intruppting the script
    fi

    local left_part=${string:0:$length-$index}
    local right_part=${string:$length-$index:$index}
    out_result="${left_part}${string_to_insert}${right_part}"
}

capture_stderr() {
  local -n stderr="${1:?}"; shift
  { stderr="$({ "$@" 1>&3; } 2>&1)"; } 3>&1
}

assert_equals() {
    local error_message=$1; shift
    local expected_result=$1; shift
    local actual_result=$1; shift

    if [ "$expected_result" != "$actual_result" ]; then
        echo "$error_message; Expected: $expected_result; Actual: $actual_result"
    fi
}

test_insert_at_position_from_end() {
    # insert to the middle
    local test_string="2278"
    local expected="2.278"
    local actual=""
    insert_at_position_from_end "$test_string" "." 3 actual
    assert_equals "test_insert_at_position_from_end-10" "$expected" "$actual"

    # insert to the beginning
    local test_string="278"
    local expected=".278"
    local actual=""  # reset from previous test
    insert_at_position_from_end "$test_string" "." 3 actual
    assert_equals "test_insert_at_position_from_end-40" "$expected" "$actual"

    # insert to the end
    local test_string="278"
    local expected="278."
    local actual=""
    insert_at_position_from_end "$test_string" "." 0 actual
    assert_equals "test_insert_at_position_from_end-50" "$expected" "$actual"

    # try to insert out of the right border
    local test_string="278"
    local actual=""
    local expected_stderr="index out of bounds error: index=-1, string length=3; expected indexes: [0..3]"
    capture_stderr actual_stderr insert_at_position_from_end "$test_string" "." -1 actual
    assert_equals "test_insert_at_position_from_end-70" "$expected_stderr" "$actual_stderr"

    # try to insert out of the left border
    local test_string="278"
    local actual=""
    local expected_stderr="index out of bounds error: index=4, string length=3; expected indexes: [0..3]"
    capture_stderr actual_stderr insert_at_position_from_end "$test_string" "." 4 actual
    assert_equals "test_insert_at_position_from_end-110" "$expected_stderr" "$actual_stderr"
}

echo "test started"

test_insert_at_position_from_end

echo "test finished"

To run test:

./insert_string.sh

No output with errors means that test have passed successfully.

  • insert_at_position_from_end — implementation of function that inserts one string into another at specified index position from the end
  • test_insert_at_position_from_end — test for this function. Examples of usages for insert_at_position_from_end function can be found here.
  • assert_equals — function that checks if expected result equals to actual. Prints error message if not.
  • capture_stderr — function that allows to capture stderr for called function.
    More extended general solution for capturing all possible outcomes of tested function to separate var:
    How to put all called function/command output results to different corresponding vars: for stderr, for stdout, for string result and for return code?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật